ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

An Introduction to the Eclipse IDE
Pages: 1, 2

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);


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:

  • Customizable key bindings, including a set of Emacs-like bindings
  • The ability to automatically turn TODO, FIXME, and tagged comments in Java code into items in the Tasks list.
  • Improved CVS support, including the ability to define "working sets" that limit the number of projects shown in the CVS Repositories view.
  • New "hovers" in the Java editor, which can be used to show source code, Javadoc, or detailed error messages for a particular line of code or expression in a pop-up "tooltip" window.
  • Support for several new refactorings
  • A new Threads and Monitors view in the debugger, which allows you to determine which threads are holding locks, which threads are waiting, and other information necessary to track down problems in multi-threaded applications.
  • Tighter integration with the Jakarta Ant build tool.

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.


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.