ONJava.com    
 Published on ONJava.com (http://www.onjava.com/)
 See this if you're having trouble printing code examples


An Introduction to the Eclipse IDE

by Scott Storkel
12/11/2002

If you closely follow open source or Java programming, you may have heard some of the buzz surrounding Eclipse. Eclipse is an extensible, open source IDE (integrated development environment). The project was originally launched in November 2001, when IBM donated $40 million worth of source code from Websphere Studio Workbench and formed the Eclipse Consortium to manage the continued development of the tool.

The stated goals of Eclipse are "to develop a robust, full-featured, commercial-quality industry platform for the development of highly integrated tools." To that end, the Eclipse Consortium has been focused on three major projects:

  1. The Eclipse Project is responsible for developing the Eclipse IDE workbench (the "platform" for hosting Eclipse tools), the Java Development Tools (JDT), and the Plug-In Development Environment (PDE) used to extend the platform.

  2. The Eclipse Tools Project is focused on creating best-of-breed tools for the Eclipse platform. Current subprojects include a Cobol IDE, a C/C++ IDE, and an EMF modeling tool.

  3. The Eclipse Technology Project focuses on technology research, incubation, and education using the Eclipse platform.

The Eclipse platform, when combined with the JDT, offers many of the features you'd expect from a commercial-quality IDE: a syntax-highlighting editor, incremental code compilation, a thread-aware source-level debugger, a class navigator, a file/project manager, and interfaces to standard source control systems, such as CVS and ClearCase.

Eclipse also includes a number of unique features such as code refactoring, automatic code updates/installs (via the Update Manager), a task list, support for unit testing with JUnit, and integration with the Jakarta Ant build tool.

Despite the large number of standard features, Eclipse is different from traditional IDEs in a number of fundamental ways. Perhaps the most interesting feature of Eclipse is that it is completely platform- and language-neutral. In addition to the eclectic mix of languages supported by the Eclipse Consortium (Java, C/C++, Cobol), there are also projects underway to add support for languages as diverse as Python, Eiffel, PHP, Ruby, and C# to Eclipse.

Related Reading

Java In a Nutshell
By David Flanagan

Platform-wise, the Eclipse Consortium provides prebuilt binaries for Windows, Linux, Solaris, HP-UX, AIX, QNX, and Mac OS X. Much of the interest in Eclipse centers around the plug-in architecure and rich APIs provided by the Plug-in Development Environment for extending Eclipse. Adding support for a new type of editor, view, or programming language is remarkably easy, given the well-designed APIs and rich building blocks that Eclipse provides.

With hundreds of plug-in development projects in progress, industry giants like IBM, HP, and Rational (just acquired by IBM) providing resources, and design heavy-weights like Erich Gamma helping to guide the process, the future indeed looks bright for Eclipse.

Installation

By now you're probably wondering where you can get a copy of Eclipse for your platform and what you have to do to install it. The first thing you need to do is make sure you have an appropriate Java runtime available. Though Eclipse can compile your code for either 1.3 or 1.4 VMs, current versions were designed to run on a 1.3 VM. If you're not sure where to find an appropriate Java VM for your platform, you can find more information at eclipse.org.

Once you have a Java VM, you're ready to get Eclipse. Visit the Eclipse downloads page and grab the latest release build for your platform. All builds are delivered as .zip files. Unzip the archive into an appropriate directory and then read through any files that may be present in the readme directory.

Believe it or not, your install is now complete. If you've installed your JVM properly and unpacked the Eclipse archive correctly, you should be ready to run Eclipse for the first time. All binary distributions provide a launch application in the primary eclipse directory. The name of the launcher changes from platform to platform: eclipse.exe on Windows, eclipse on Solaris, and so on. The first time the Eclipse application is run, it will complete a few remaining install tasks (i.e., creation of a workspace directory for storing project files) before the environment comes up.

Interface

Once you have Eclipse installed and running, it's time to begin using it. After you initially launch Eclipse, you should see a window which looks something like this:

Click for larger view
Initial Eclipse window layout (You can click on the screen shot to open a full-size view.)

As you can see, the Eclipse IDE has a fairly standard menu bar:

Eclipse menu bar

and toolbar:

Eclipse toolbar

There is also a tabbed editor, currently showing the Welcome page:

Eclipse tabbed editor

Sample App

The File Navigator, Code Outline, and Task List are present, but don't contain any data yet. In order to continue our exploration of Eclipse, we'll create a simple Swing-based calculator program.

  1. Select File->New->Project... to begin creating your first Java project. In the wizard that appears, select Java from the list of possible project types in the left-hand list and Java Project from the right-hand list, then click the Next button.

    First page of the New Project wizard
    First page of the New Project wizard

  2. For the project name, enter Calculator, then click the Next button again.

    Second page of the New Project wizard
    Second page of the New Project wizard

  3. The final step of the wizard allows us to specify where source code and class files should live, specify any subprojects which might be needed by the current project, and any provide any libraries that may be necessary for the build. Select the Use source folder contained in the project radio button, then click the Create New Folder... button. Enter src for the name of the new folder. Click Yes when Eclipse asks if you want to update the Build output folder to Calculator/bin.

    Third page of the New Project wizard
    Third page of the New Project wizard

  4. Click the Finish button to have Eclipse create the project.

Once your project has been created, you may notice that the layout of the Eclipse window doesn't look quite the same as it did before you created the project: the Outline view has been moved to the left side of the window, the Navigator has been replaced with a Package Explorer, and so on.

Click for larger view
Java perspective (You can click on the screen shot to open a full-size view.)

This new layout is called the Java Perspective. A perspective, in Eclipse parlance, is a saved layout containing any number of different editors and views. Eclipse ships with a number of default perspectives (Resource, Java, Debug, etc.) that can be customized, or you can create completely new perspectives. Perspectives are managed using items from the Window menu or the perspective toolbar which normally appears along the left-hand edge of the Eclipse window.

Perspective selection toolbar

The next step in creating our Java project is to create the directories that will contain our source code. Switch to the Resource perspective using Window->Open Perspective->Resource. In the Navigator view, expand the folder tree until you see the src node. Select this node, then choose the File->New->Folder menu item. In the dialog that appears, make sure that the src directory is selected, then enter com in the Folder Name field.

Creating large numbers of folders using the New Folder dialog can be a bit cumbersome. Luckily, Eclipse is just as happy to let you do the work using your favorite filesystem interface: command shell, Windows Explorer, etc. Use one of these methods to create folders named devious and calculator under the com folder. Once you've done this, select any of the folders in the Navigator view and select the File->Refresh menu item. Eclipse will examine the filesystem and update the project to match. At this point, your project should look something like this:

Click for larger view
Folder creation complete (You can click on the screen shot to open a full-size view.)

One final note on file creation: in the early stages of a project, you may find yourself frequently running code, creating a new file or folder, adding some more code, compiling, and testing the code again. It's important to remember that the menus Eclipse displays are specific to a given perspective. So, when you switch to the Resource perspective to create a new file or folder in the Navigator view, you'll find that the Run menu, for instance, changes dramatically from the Run menu in the Java perspective. One solution to this particular problem is to use the Window->Show View menu to show the Navigator view in the Java perspective. If you want to make this a permanent change, you can use the Window->Save Perspective As... menu item to save your changes as either a new perspective or to replace the existing Java perspective.

Adding Code

Now that we have the directory structure in place, we can begin adding code to our project. We'll divide our calculator program into three different files: CalcModel.java, CalcPanel.java, and Calculator.java. Grab the .zip archive and place these files in the com/devious/calculator directory. Use File->Refresh to update the Eclipse project.

Click for larger view
Project with source files added (You can click on the screen shot to open a full-size view.)

CalcPanel.java handles most of the user interface for our calculator. Calculator.java handles setup and launching of the GUI. Most of the important work happens in CalModel.java, which implements the actual "mechanics" of our calculator: responding to events, performing numerical calculations, updating the display, and so on. The easiest way to explore the code is by switching to either the Java perspective or the Java Browsing perspective. Use the Open Perspective item in the Window menu or click the Open button on the perspective toolbar.

As you use the Package Explorer to browse the source code, you may notice that the Outline view seems a bit superfluous. You can click the X in the upper right corner of the Outline view to close it. Should you want to see it again, you can use the Window->Show View->Outline menu item to display it again. Those interested in a multi-paned source code viewer a la Smalltalk should give the Java Browsing perspective a try.

Click for larger view
Java Browsing perspective (You can click on the screen shot to open a full-size view.)

Running Code and Debugging

So you've managed to download and install Eclipse, create a Java project, and add the folders and files needed for a simple application. Now it's time to see the application run. You'll need to use the Java perspective to complete the steps necessary to run the app:

  1. From the Run menu, select the Run... item.
  2. In the wizard dialog which appears, select "Java Application" from the list of Launch Configurations, then click the New button.

    Click for larger view
    (You can click on the screen shot to open a full-size view.)

  3. Enter Calculator as the Name for the new configuration.

  4. Click the Search... button and select Calculator as the Main class, or simply enter com.devious.calculator.Calculator in the text entry box.

    Click for larger view
    (You can click on the screen shot to open a full-size view.)

  5. Click the Run button to save the configuration and run the application.

Once you've created a launch configuration for the application, you can run it again using Run->Run History menu item or the Run button on the toolbar. If you spend enough time playing with the calculator, you'll notice that while it works pretty well, it does seem to have one major flaw: it won't do subtraction! Time to fire up the Eclipse source-level debugger and find the problem. If you've been following along with the examples, you should be able to debug the application using the same launch configuration defined earlier. Select Run->Debug History->Calculator to launch the application in the debugger.

When you launch your application for debugging, Eclipse automatically switches to the Debug perspective:

Click for larger view
Debug perspective (You can click on the screen shot to open a full-size view.)

Since our problem seems to center around the subtraction function, we'll set breakpoints in the event handler for the minus key and the equals key. First, we need to switch to the editing tab for the CalcPanel class. (If you don't have an open tab for CalcPanel, you'll have to switch back to the Resource or Java perspective, open the file there, then switch back to the Debug perspective.)

Use the Outline view to select the actionPerformed method. Scroll down a few lines until you see the case statement for the minus key (line 126). If you double-click in the left margin at this line, you'll see that Eclipse sets a breakpoint there. Scroll down a bit more and set another breakpoint on the call to model.calculate() in the handler for the equals key (line 138).

Click for larger view
Breakpoint set (You can click on the screen shot to open a full-size view.)

Next, switch to the calculator window and click some of the buttons. Something like 2 4 6 - should do the trick. Once the debugger hits the breakpoint, you can use the buttons in the toolbar for the Debug view to step into, over, or out of methods.

Debug toolbar

If we step into the setOperation method, we see that it just saves the operation code to the pendingOp field. Press the Resume button on the Debug toolbar to continue the execution of the program.

Tip: A common mistake for new Eclipse users is to press the Debug or Run buttons on the main toolbar rather than the Resume button. This will launch another copy of your application, rather than continuing the current one.

Now, click a few more buttons (e.g. "1", "2", "3") and then the equals key. Once we step into the calculate method, the problem is obvious: someone copied the code from the OP_ADD case but didn't edit it to properly perform a subtraction rather than an addition. Problem solved!

Hotswap Bug Fixing

If you happen to be running your application using a 1.4 JVM, there's one final step to our debugging example. Rather than exiting the app, changing the code, recompiling, and then starting another debugging session, we can fix the problem on the fly. With the debugger still running, change the problem line from:

setDisplay(previousValue + currentValue);

to

setDisplay(previousValue - currentValue);

Save the file, then press the Resume button to get the application running again. Use the calculator's C button to reset it, then try the same sequence of operations again. This time around, it works!

What we've just demonstrated is Eclipse's support for a new 1.4 feature called "HotSwap." The Java Platform Debugger Architecture (JPDA) now supports the ability to substitute modified code in a running application. This is particularly useful when starting your application, or getting to the point where it fails, takes a long time.

A Taste of Things to Come

One very attractive feature of Eclipse is the speed with which product bugs and deficiencies are addressed. While Eclipse 2.0.2 is the current version of the product, the Eclipse Group is actively working toward the release of Eclipse 2.1. Monthly "milestone" builds (M1, M2, M3, etc.) deliver the latest stable feature set from Eclipse 2.1 to interested users. Among the features included in the latest 2.1 M3 build:

It is this last feature which makes Eclipse particularly well-suited to Java application development and, in particular, Web application development. Eclipse brings together all of the tools a developer needs to be successful at Web application development: an extensible IDE, a standards-based compiler, remote debugging, Ant-based builds, JUnit-based testing, and plug-ins for communicating with most application servers and EJB containers.

Resources

Scott Storkel is an experienced programmer with over twenty years of experience, including object-oriented systems for the last twelve years and building enterprise-class Java web applications for the last four years.


Return to ONJava.com.

Copyright © 2009 O'Reilly Media, Inc.