Using the BlueJ Debugger
BlueJ is an integrated Java environment specifically designed for introductory teaching. It is freely available from It runs on Windows, Macintosh, Linux/Unix and other systems. We can find a good tutorial on its use at . BlueJ is just a development environment. It requires a prior installation of the Java 2 SDK version 1.3 or newer.
Many development environments include a debugger, which is an application used to debug a program. The BlueJ debugger is especially easy to use and suitable for an introductory programming course.
To illustrate the use of the BlueJ debugger, we use a modification of Example 3.8 in which we fix the high integer at 5.[1]
Example 3.11 MistakeDebugger.java
/* Incorrect attempt to sum the squares of
* numbers from 1 to 5.
*/
public class MistakeDebugger {
// Use a loop to compute a sum of squares
public static void main(String [] args)throws IOException {
int sum = 0; // Current value to square and add
int count = 1;
int high = 5;
while (count <= high)
sum += count*count;
System.out.println("The sum of the first " + high +
" squares is " +sum);
}
}
------
First, we need to create a project. We create separate directory for each project. Because we already have a source file, we will open a non-BlueJ project. After opening BlueJ, we click Project and Open Non-BlueJ Project and navigate to the directory that contains Example 3.11. Pressing Open in BlueJ brings up the window of Figure 3.11.
Figure 3.11 The Mistake project in BlueJ
BlueJ initially draws the MistakeDebugger icon in Figure 3.11 with stripes, meaning that we have not yet compiled it. Pressing the Compile button will compile Example 3.11, removing the stripes. To run Example 3.11 from BlueJ, we right-click on the MistakeDebugger icon, and select the main method, void main(args), from the popup menu. Clicking OK in the Method Call window will start Java executing the main method.
A striped horizontal bar at the lower left indicates that the program is running. Clicking that turning symbol will open the debugger window. Because we expected output and nothing is happening, we press the Halt button to interrupt execution producing the debugger window of Figure 3.12.
Figure 3.12 The Debugger Window
The debugger window shows the values of the local variable when we interrupted execution. It shows that sum is 937130433 and count is 1. Because count is still 1, we can probably deduce that we forgot to increment it in the loop body so the loop will eventually terminate.
To see the behavior of the program even more clearly we can execute one line at a time. In the debugger window, the Step button causes Java to execute one line of code each time we press it. If that line calls a method, the debugger executes the method in its entirety. The Step Into button works the same way as the Step button, except that it allows us to execute each line of the code in the method too. We press Step Into when we think that code in the method might be causing the problem.
Pressing the Step button brings up an editor window of Figure 3.13 with an arrow pointing to the next line to execute. The arrow points to the statement that adds the next square to the sum. Pressing Step again causes the sum to increase by one, giving us more evidence of the problem.
Figure 3.13 Single-stepping in the debugger
Having identified the problem, we press the Terminate button to stop execution. Using the editor window that is already open, we can fix the program, compile, and run while still in BlueJ. This time the result shown in Figure 3.14 appears.
Figure 3.14 The result of executing the revised Example 3.11
Another useful debugger feature allows us to set a breakpoint at which the program will stop while executing. We might set a breakpoint at a line that is close to where we think the problem is. When the program stops, we can use the single step feature to help us find the problem.
To set a breakpoint in Example 3.11, we open the project in BlueJ and right click on the MistakeDebugger icon. Selecting Open Editor opens a window like Figure 3.13. Clicking the mouse in the margin to the left of the sum += count*count; statement cause a Stop sign to appear in the margin indicating that we have set a breakpoint. When we execute the main method, execution will stop when it reaches that line and a debugger window like Figure 3.12 will appear. We can then single step execution and track the changes in the variable values to determine if the program is executing as we expect.
We used a simple example to illustrate the BlueJ debugger. The debugger becomes especially helpful when developing larger projects.
[1]In Windows, BlueJ version 1.2.0 puts the Swing dialog in Example 3.8 behind the BlueJ window. We must move the BlueJ window to see it.