Eclipse

Eclipse is a free open-source Java environment available from http://eclipse.org. Eclipse is a Java program, but it uses a custom user interface toolkit that does not run on all platforms that supports Java 2. Check the web site for supported platforms. Eclipse requires a Java 2 runtime, so you need to install the Java 2 SDK first before installing Eclipse. You can download the SDK from http://java.sun.com/j2se/.

Starting Eclipse

When you start Eclipse, a startup screen appears, and the program spends some time loading various modules.

.

When Eclipse has finished loading, you see a screen similar to the following:

.

Setting preferences

It is a good idea to set preferences for the Java editor that match the conventions of the textbook. Select Window->Preferences from the menu. Select Java->Appearance->Code Formatter from the tree in the left panel.

Then check the boxes Insert a new line before an opening brace and Insert new lines in control statements.

.

Click on the Style tab. Then uncheck Insert tabs for indentation, not spaces. Set Number of spaces representing an indentation level to 3. (The Eclipse default is 4.)

.

Click the OK button to save your settings.

Loading an existing program

If you already have your program in a Java file (or a directory containing multiple Java files), then you need to make a project that contains the file. Follow these steps.

Select File->New->Project from the menu. You will get the following dialog.

.

Select the Java option and click on the Next> button. (Do not select the Simple option!)

In the following dialog, give a name to the project. A good choice for the name is the directory that contains the files. Then uncheck the Use default box, and provide the full path of the directory that contains the files, such as

/home/cay/bigj/ch02/greeter1/
or
c:\bigj\ch02\greeter1\

.

Click on the Finish button.

The project appears in the left hand panel. Expand it, and also expand the default package icon. Double-click on one of the file names. The file is displayed in an edit window:

.

Starting a new program

If you write a program from scratch, then you can start your work in Eclipse. It is always best to place each of your programs into a separate directory.  Eclipse will create the directory for you.

Select File->New->Project from the menu. You will get the New Project dialog.

Select the Java option and click on the Next> button. (Do not select the Simple option!)

In the following dialog, give a name to the project. A good choice for the name is the directory that contains the files. Then uncheck the Use default box, and provide the full path of the directory that contains the files, such as
/home/cay/hw1/
or
c:\homework\hw1\

.

Click on the Finish button. Now locate the name of your new project in the left hand panel. Click on it with the right mouse button. Select New->Class from the menu.
.

The New Class dialog appears.

Supply the name of the class. If you want a main method for this class, check the box public static void main(String[] args).

.

Click on the Finish button.

Finally, you get an editor window into which you can type your program.

.

As you type in your program, occasionally select File->Save from the menu to save your work.

You may enjoy the "content assist" feature of Eclipse. If you type a partial input and then hit CTRL+SPACE, a dialog shows all possible completions. Just pick the one you want from the list.

.

You may also want to experiment with some of the other convenience features of Eclipse, such as Source->Generate getter and setter.

Compiling a program

To compile a program in Eclipse, select the project in the pane on the left hand side. Then select Project->Rebuild Project from the menu.

Compilation errors are displayed in a window at the bottom of the Eclipse frame.

Click on an error message, and the cursor moves to the offending line in the edit window:

.

Running a program

To run a program, select the Run->Run as...->Java Application menu option. The program runs. Any console output is directed to a window at the bottom of the screen.

.

Running applets

To run an applet, first compile the program as previously described. Make sure the current project is still selected in the leftmost pane. Then select the menu option Run->Run as...->Java Applet. Eclipse will launch the applet viewer on the subclass of Applet that it finds in your project.

.

The applet viewer is started with a default size which is rather small. Just resize the applet window.

Close the window to terminate the applet.

Generating Javadoc comments

Select Project -> Generate Javadoc from the menu. You will get a dialog such as the following one:

.

Click the cleckbox for the project that you want to document. Then make sure that the destination directory is correct. Click on Finish.

Tracing through a program.

Before debugging a program, you should set a breakpoint at the start of the main method.

Double-click on the gray bar to the left of the edit window, next to the first source line after the line public static void main(String[] args). A blue dot appears, indicating the breakpoint.

.

Then select the menu option Run->Debug as...->Java Application. The debugger starts and switches the Eclipse display to the debugger perspective. It pauses at the breakpoint that you set.

.

Whenever you select the menu option Run->Step Over (or the F6 keyboard shortcut), then the debugger executes one line of the program, without stepping inside method calls. For example, tracing over the call

Word w = new Word(token);

will not trace inside the Word constructor but simply run the program to the next line of the main method.

Contrast that with the menu option Run->Step Into(or the F5 keyboard shortcut). This command traces inside method calls. For example, tracing into the line

int syllables = w.countSyllables();

stops at the first line of the countSyllables method:

.

Watching values

To see the value of a variable of a simple type (such as a number or a string), simply hold the mouse pointer over the variable name when the debugger is stopped. Then the contents of the variable is displayed in a small rectangle next to the variable name. For example, here is the contents of the count variable in the countSyllables method.

.

The top right window shows all local variables. Click on the triangles to look inside objects. To see the instance fields of the implicit parameter, look inside this.

.

The top left window shows the call stack (with the most recently called method on top). For example, the following call stack shows that the WordTest.main method called the Word.<init> method, that is, the constructor.

.

Setting breakpoints

Tracing through a program can be tedious. Often, you want the program to run at full speed until a particular line of code is encountered. To set a breakpoint at a line, double-click on the gray bar to the left. A blue dot indicates the breakpoint.

.

Now select the menu option Run->Resume or hit the F8 keyboard shortcut. The program runs to the next breakpoint, stopping only for user input.

You can set as many breakpoints as you like.

To remove a breakpoint, double-click on it with the mouse.

Stopping the debugger

When the program has completed, the debugger stops automatically. When you want to terminate a debugging session without running the program to the end, select the menu option Run->Terminate.

To return to the Java perspective (in which you can edit and compile your program), locate the perspectives toolbar at the left of the Eclipse frame:

.

Click on the button with the "J" (above the bug button). Eclipse will remove the debugger windows and restore the edit windows.