Debugging a Java Program with Eclipse

Video Demonstration

One of the benefits that Eclipse provides is the ability to run code interactively by using its integrated debugger. Examining variables and expressions while executing code step-by-step is an invaluable tool for investigating problems with your code. This excerpt from Chapter 2 of "Eclipse in Action: A guide for Java developers" provides an introduction to creating a Java project, running a Java program, and debugging it.

To prepare for debugging, you need to set a breakpoint in your code so the debugger suspends execution and allows you to debug - otherwise, the program will run to completion without letting you do any debugging. For this example I am using code to calculate a student's final numeric grade based on their individual assignment grades in a weghted average.To set a breakpoint, double-click in the gray margin on the left side of the editor, maybe next to the statement "quiz1 = input.nextInt();". A blue dot will appear, indicating an active breakpoint.

Starting the program under the debugger is similar to running it. Eclipse provides two options: Use the full-service Run->Debug menu selection to use a launch configuration, or use the express Run->Debug As->Java Application selection if the default options are okay. Eclipse will start the program, change to the Debug perspective, and suspend execution at the breakpoint.

The Debug perspective includes several new views that are, not surprisingly, especially useful for debugging. First, at top left, is the Debug view (not to be confused with the Debug perspective to which it belongs), which shows the call stack and status of all current threads, including any threads that have already run to completion. Your program, which Eclipse started, has hit a breakpoint, and its status is shown as Suspended.

In the title bar of the Debug view is a toolbar that lets you control the program's execution. The first few tool buttons, which resemble the familiar controls of electronic devices such as CD players, allow you to resume, suspend, or terminate the program. Several buttons incorporate arrows in their design; these allow you to step through a program a line at a time. Holding the mouse over each button in turn will cause tool tips to appear, identifying them as Step With Filters, Step Into, Step Over, Step Return, and so on.

For example, click the "StepOver" button so it executes the "quiz1 = input.nextInt();" statement. You normally want to "StepOver" methods of the standard Java packages or third-party packages. Go down to the Console view and type in a grade for Quiz1 and press enter. Continue with pressing the "StepOver" button and entering input (and a press of enter) for each prompt. You will see each variable and its value appear in the Variables view in the upper right corner. Stop when you get to (but do not execute) the "grade = 0.1*quiz1+0.15*quiz2+0.25*midterm+0.3*finalExam+0.2*labGrades;" statement.

Sometimes a program has many variables, but you're interested in only one or a few. To watch select variables or expressions, you can add them to the watch list in the Expression view. To do this, select a variable - "grade", for instance - by double-clicking on it in the editor, and then right-clicking on the selection and choosing Watch from the context menu. The variable (and its value, if it's in scope) will appear in the Expressions view. Before we execute the current statement the "grade" variable cannot be resolved.

One significant advantage of watching variables in the Variables and Expressions views over using print statements for debugging is that you can inspect objects and their fields in detail and change their values even normally immutable strings. Return to the Variables view and select the "quiz1" variable so its value is shown below. Replace the current value for "quiz1" with a new value, rightclick, and choose "AssignValue". The value of "quiz1" is changed.

Click on "StepInto" (or "StepOver", since there is no method call on the current statement it makes no difference which you choose), and see the "grade" expression evaluated and value stored.

In the Expression view, you can also enter and evaluate arbitrary expressions including any variables. Right click in the Expression view and choose "Add Watch Expression". Type in quiz1+quiz2 and press OK. You will see the new expression added to the view and evaluated immediately. This can be really helpful when checking expressions with mixed datatypes.

To have your program run to completion, press the green resume button. Switch back to the "Java Perspective" (upper right) to get back to the main editor.

Copyright CS, Illinois Institute of Technology, Spring 2006