devxlogo

Getting Started with the Eclipse Workbench

Getting Started with the Eclipse Workbench

he Eclipse Workbench, at the most fundamental level, is merely a platform for software development tools and provides little functionality on its own. With the appropriate plug-in components, however, it’s capable of supporting any type of software development. It is most commonly used for developing using the Java programming language, in large part because the SDK version of Eclipse, (which you can download from the Eclipse site), includes the Java Development Toolkit (JDT) which, together with the basic Eclipse platform, provide a full-featured Java IDE.

Eclipse is easy to use, but the key to using it effectively is to understand the way it is organized. This excerpt from Chapter 2 of Eclipse in Action: A guide for Java developers (Manning Publications) is designed to get you up to speed quickly by introducing you to the basic elements of Eclipse user interface?perspectives, views, and editors?and showing you how to create a Java project.

A Tour of the Workbench UI
Eclipse is made up of components, and the fundamental component is the Eclipse Workbench. This is the main window that appears when you start Eclipse. The Workbench has one simple job: to allow you to work with projects. It doesn’t know anything about editing, running, or debugging Java programs; it only knows how to navigate projects and resources (such as files and folders). Any tasks it can’t handle, it delegates to other components, such as the Java Development Tools (JDT).

Perspectives, Views, and Editors
The Eclipse Workbench is a single application window that at any given time contains a number of different types of panes called views plus one special pane, the editor. In some cases, a single pane may contain a group of views in a tabbed notebook. Depending on the perspective, one pane might contain a console window while another might contain an outline of the currently selected project. The primary component of every perspective, however, is the editor.

Just as there are different types of documents, there are different types of editors. When you select (or create) a document in Eclipse, Eclipse does its best to open the document using the most appropriate editor. If it’s a simple text document, the document will be opened using Eclipse’s built-in text editor. If it’s a Java source file, it will be opened using the JDT’s Java editor, which has special features such as the ability to check syntax as code is typed. If it’s a Microsoft Word document on a Windows computer and Word is installed, the document will be opened using Word inside Eclipse, by means of object linking and embedding (OLE).

You don’t directly choose each of the different views in the Workbench or how they are arranged. Instead, Eclipse provides several preselected sets of views arranged in a predetermined way; they are called perspectives, and they can be customized to suit your needs.

Dragging one view on top of another will cause them to appear as a single tabbed notebook of views.
Every perspective is designed to perform a specific task, such as writing or debugging a Java program, and each of the views in the perspective is chosen to allow you to deal with different aspects of that task. For example, in a perspective for debugging, one view might show the source code, another might show the current values of the program’s variables, and yet another might show the program’s output.

The first time you start Eclipse, it will be in the Resource perspective (see Figure 1). You might think of this as the home perspective. It is a general-purpose perspective useful for creating, viewing, and managing all types of resources?whether a resource is a Java project or a set of word processing documents doesn’t matter in this perspective, apart from which editor is used to open specific documents in the editor area.

The panel at upper left is called the Navigator view; it shows a hierarchical representation of your workspace and all the projects in it. At first this view will be empty, of course; but, as you’ll see, it is the starting point for creating projects and working with Eclipse.

Figure 1. The Resource Perspective: The initial view is a general-purpose perspective for creating, viewing, and managing all types of resources.

Within the Workbench, as you work, you can choose among the different perspectives by selecting Window > Open Perspective. Eclipse will also change the perspective automatically, when appropriate?such as changing from the Java perspective to the Debug perspective when you choose to debug a program from the Eclipse menu.

Menus and Toolbars
In addition to perspective, views, and editors, several other features of the Workbench UI are worth mentioning: the main menu, the main toolbar, and the shortcut toolbar. Like the views and editors in a perspective, the Workbench’s menu and toolbar can change depending on the tasks and features available in the current perspective.

The Eclipse main menu appears at the top of the Workbench window, below the title bar (unless you are using a Macintosh, in which case the menu appears, Mac style, at the top of the screen). You can invoke most actions in Eclipse from the main menu or its submenus. For example, if the document HelloWorld.java is currently being edited, you can save it by selecting File > Save HelloWorld.java from the main menu.

Below the main menu is a toolbar called the main toolbar, which contains buttons that provide convenient shortcuts for commonly performed actions. One, for example, is an icon representing a floppy disk, which saves the contents of the document that is currently being edited (like the File > Save menu selection). These tool buttons don’t display labels to indicate what they do unless you position the mouse pointer over them; doing so causes a short text description to display as a hovering tool tip.

Along the left side of the screen is another toolbar called the shortcut toolbar. The buttons here provide a quick way to open a new perspective and switch between perspectives. The top button, Open a Perspective, is an alternative to the Window > Open Perspective selection in the main menu. Below it is a shortcut to the Resource perspective. As you open new perspectives, shortcuts to those perspectives appear here, as well.

You can optionally add another type of shortcut to the shortcut toolbar: a Fast View button. Fast Views provide a way to turn a view in a perspective into an icon?similar to the way you can minimize a window in many applications. For example, you may find that in the Resource perspective, you need to look at the Outline view only occasionally. To turn the Outline view into a Fast View icon, click on the Outline icon in the view’s title bar and select Fast View from the menu that appears. The Outline view is closed, and its icon appears in the shortcut toolbar. Clicking on the icon alternately opens and closes the view. To restore the view in its previous place in the perspective, right-click on the Fast View icon and select Fast View.

In addition to the Workbench menu and toolbars, views can also have menus. Every view has a menu you can select by clicking on its icon. This menu lets you perform actions on the view’s window, such as maximizing it or closing it. Generally this menu is not used for any other purpose. Views can also have a view-specific menu, which is represented in the view’s title bar by a black triangle. In the Resource perspective, the Navigator view has a menu that lets you set sorting and filtering options.

Some views also have a toolbar. In the Resource perspective, the Outline view has tool buttons that let you toggle various display options on or off.

Changing Perspectives
As you work in the Eclipse Workbench, you’ll occasionally find that the different views aren’t quite the right size for the work you’re doing?perhaps your source code is too wide for the editor area. The solution is to click on the left or right window border and drag it so the window is the right size.

Sometimes you may want to supersize a view temporarily by double-clicking on the title bar; this will maximize it within the Eclipse Workbench. Double-clicking on the title bar again will reduce it back to its regular size.

You can also move views around by dragging them using their title bars. Dragging one view on top of another will cause them to appear as a single tabbed notebook of views. Selecting a view in a notebook is like selecting a document in the editor pane: Click its tab at the top or bottom of the notebook. Dragging a view below, above, or beside another view will cause the views to dock?the space occupied by the stationary view will be redistributed between the stationary view and the view you are dragging into place. As you drag the window you want to move, the mouse pointer will become a black arrow whenever it is over a window boundary, indicating that docking is allowed. For example, if you want to make the editor area taller in the Resource perspective, drag the Task view below the Outline view so the Navigator, Outline, and Task views share a single column on the left side of the screen.

In addition to moving views around, you can remove a view from a perspective by selecting Close from the view’s title bar menu. You can also add a new view to a perspective by selecting Window > Show View from the main Eclipse menu.

Eclipse will save the changes you make to perspectives as you move from perspective to perspective or close and open Eclipse. To restore the perspective to its default appearance, select Window > Reset Perspective.

If you find that your customized perspective is particularly useful, you can add it to Eclipse’s repertoire of perspectives. From the Eclipse menu, select Window > Save Perspective As; you will be prompted to provide a name for your new perspective.

Creating and Running a Java Program
Now that you understand how the different views in perspectives work together to allow you to perform a task, let’s take Eclipse out for a spin by writing and running a traditional “Hello, world” program.

Before you can do anything else in Eclipse, such as creating a Java program, you need to create a project. To create a new Java project, follow these steps:

  1. Right-click in the Navigator view to bring up a context menu and select New > Project.
  2. In the New Project dialog box, Eclipse presents the project options: Java > Plug-in Development, and Simple. Because you want to create a Java program, select Java on the left side of the dialog box.
  3. Select Java Project on the right. If you’ve installed other types of Java development plug-ins, various other types of Java projects may potentially be listed here (EJBs and servlets, for example). But the JDT that comes standard with Eclipse only offers support for standard Java applications, so you must choose the Java Project option.
  4. Click Next to start the New Java Project Wizard.
  5. The first dialog box prompts you for a project name. This is a simple “Hello, world” example, so enter “Hello.” Clicking Next would take you to a dialog box that lets you change a number of Java build settings, but for this example you don’t need to change anything.
  6. Click Finish.
  7. Eclipse notifies you that this kind of project is associated with the Java perspective and asks whether you want to switch to the Java perspective. Check the “Don’t Show Me This Message Again” box and click Yes.

The perspective changes to a Java perspective (see Figure 2). Notice that the view in the upper-left corner is no longer the Navigator view; it is now the Package Explorer view, and it displays the new Hello project. The Package Explorer is similar to the Navigator, but it’s better suited for Java projects; for one thing, it understands Java packages and displays them as a single entry, rather than as a nested set of directories. Notice also that a new icon has appeared on the left edge of the Workbench: a shortcut for the Java perspective.

Figure 2. The Package Explorer View: This perspective is better suited for Java projects because it displays Java packages as a single entry instead of a nested set of directories.

At the bottom of the window is a Tasks view. It is useful for keeping track of what needs to be done in a project. Tasks are added to this list automatically as Eclipse encounters errors in your code. You can also add tasks to the Task view by right-clicking in the Tasks view and selecting New Task from the context menu; this is a convenient way to keep a to-do list for your project.

Finally, notice the Outline view on the right side of the screen. The content of this view depends on the type of document selected in the editor. If it’s a Java class, you can use the outline to browse class attributes and methods and move easily between them. Depending on whether the Show Source of Selected Element button in the main toolbar is toggled on or off, you can view your source as part of a file (what is sometimes referred to as a compilation unit) or as distinct Java elements, such as methods and attributes.

Creating a Java Class

The Eclipse JDT includes a special incremental compiler and evaluates your source code as you type it.
Once you’ve created a project for it to live in, you can create your first Java program. Although doing so is not necessary, it’s a good practice to organize your Java classes into packages. It’s conventional to use a domain name as the package name because this reduces the likelihood of name collisions?that is, more than one class with exactly the same name. You can use a registered domain name if you have one, but if not, you can use any convenient, unique, ad hoc name, especially for private use. Here, we’ll use org.eclipseguide. To this, add a name for this particular project: hello. All together, the package name is org.eclipseguide.hello.

Follow these steps to create your Java program:

  1. Right-click on the project and select New > Class to bring up the New Java Class Wizard.
  2. The first field, Source Folder, is by default the project’s folder?leave this as it is.
  3. Enter org.eclipseguide.hello in the Package field.
  4. In the class name field, enter HelloWorld.
  5. In the section “Which Method Stubs Would You Like to Create?,” check the box for public static void main(String[] args). The completed New Java Class dialog box is shown in Figure 3.
  6. Click Finish and the New Java Class Wizard will create the appropriate directory structure for the package (represented in the Navigator by the entry org.eclipseguide.hello under the Hello project) and the source file HelloWorld.java under this package name.
Figure 3. Hello World: the HelloWorld class using the New Java Class Wizard is shown.

In the editor area in the middle of the screen, you see the Java code generated by the wizard. Also notice that tabs now appear at the top of the editor area, which allow you to select between the Welcome screen that first appeared and this new HelloWorld.java file. (You don’t need the Welcome screen anymore, so you can click on the Welcome tab and click the X in the tab to make it go away.) You may also want to adjust the size of your windows and views to get a more complete view of the source code and the other views.

The code that’s automatically generated includes a method stub for main(). You need to add any functionality, such as printing your “Hello, world!” yourself. To do this, alter the code generated by Eclipse by adding a line to main() as follows:

  /*   * Created on Feb 14, 2003   *   * To change this generated comment go to   * Window>Preferences>Java>Code Generation>Code and Comments   */  package org.eclipseguide.hello;  /**   * @author david   */  public class HelloWorld {        public static void main(String[] args) {              System.out.println("Hello, world!");        }  }

Running the Java program
You’re now ready to run this program. There are several things you might want to consider when running a Java program, including the Java runtime it should use, whether it will take any command-line parameters, and, if more than one class has a main() method, which one to use. The standard way to start a Java program in Eclipse is to select Run > Run from the Eclipse menu. Doing so brings up a dialog box that lets you configure the launch options for the program; before running a program, you need to create a launch configuration or select an existing launch configuration.

For most simple programs, you don’t need a special launch configuration, so you can use a much easier method to start the program: First make sure the Hello World source is selected in the editor (its tab is highlighted in blue) and then do the following from the Eclipse menu:

  1. Select Run > Run As > Java Application.
  2. Because you’ve made changes to the program, Eclipse prompts you to save your changes before it runs the program. Click OK.
  3. The Task view changes to a Console view and displays your program output (see Figure 4).
Figure 4. Displaying the Output: The Eclipse Console view displays the output from the HelloWorld program.

You may wonder why no separate step is required to compile the .java file into a .class file. This is the case because the Eclipse JDT includes a special incremental compiler and evaluates your source code as you type it. Thus it can highlight things such as syntax errors and unresolved references as you type. (Like Eclipse’s other friendly features, this functionality can be turned off if you find it annoying.) If compilation is successful, the compiled .class file is saved at the same time your source file is saved.

The key to using Eclipse effectively is to understand its organizational concepts of perspectives, views, and editors. The different views that appear at one time on the Workbench, which together are called a perspective, are especially selected to enable you to accomplish a specific task, such as creating a Java project, working with source files, and running a program.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist