Java Debugging with Eclipse: A Comprehensive Guide, Study notes of Programming Languages

A step-by-step guide on how to use eclipse's debugger to identify and fix issues in java code. It covers starting the debugger, adding breakpoints, stepping through code, and evaluating variables and expressions. It also includes tips on using step filters and the console view.

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-bg5
koofers-user-bg5 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Java Debugging with Eclipse
Learn how to use the debugger. Don’t be “too busy sawing to sharpen the saw”!
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. Here you can use the latter.
Create the following class:
public class HelloWorld {
public static void main(String[] args) {
String msg = "Hello World";
System.out.println(msg);
for(int i = 0; i<5; i++){
System.out.println("debug test " + i);
}
}
Save the file and run the program. When you want to run it again, you can run it again by pressing
Ctrl+F11 or by clicking Run on the toolbar. The console will print out the following:
Hello world
debug test 0
debug test 1
debug test 2
debug test 3
debug test 4
In order to debug something, we need to add a breakpoint to the class. Add a breakpoint by double-
clicking in the left margin of the text editor on the line that prints out "debug test". A blue dot will appear
in the left margin. Press F11 to run HelloWorld again. This time, since there is a breakpoint, Eclipse
switches to the Debug perspective. The Debug perspective opens and highlights the line of code with the
breakpoint.
Another way of starting to debug is to make sure the source for HelloWorld is selected in the editor
and select Run->Debug As->Java Application from the main menu. Eclipse will start the program,
change to the Debug perspective, and suspend execution at the breakpoint. The best way of
starting the debugger (especially when you have multiple projects) is to right click on the java file
that contains the main program and select Run As or Debug As. I appeared to have less trouble with
it running the wrong project that way.
The Debug perspective includes several new views that are 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.
pf3
pf4

Partial preview of the text

Download Java Debugging with Eclipse: A Comprehensive Guide and more Study notes Programming Languages in PDF only on Docsity!

Java Debugging with Eclipse

Learn how to use the debugger. Don’t be “too busy sawing to sharpen the saw”!

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. Here you can use the latter. Create the following class: public class HelloWorld { public static void main(String[] args) { String msg = "Hello World"; System.out.println(msg); for(int i = 0; i<5; i++){ System.out.println("debug test " + i); } }

Save the file and run the program. When you want to run it again, you can run it again by pressing

Ctrl+F11 or by clicking Run on the toolbar. The console will print out the following:

Hello world

debug test 0

debug test 1

debug test 2

debug test 3

debug test 4

In order to debug something, we need to add a breakpoint to the class. Add a breakpoint by double-

clicking in the left margin of the text editor on the line that prints out "debug test". A blue dot will appear

in the left margin. Press F11 to run HelloWorld again. This time, since there is a breakpoint, Eclipse

switches to the Debug perspective. The Debug perspective opens and highlights the line of code with the

breakpoint.

Another way of starting to debug is to make sure the source for HelloWorld is selected in the editor and select Run->Debug As->Java Application from the main menu. Eclipse will start the program, change to the Debug perspective, and suspend execution at the breakpoint. The best way of starting the debugger (especially when you have multiple projects) is to right click on the java file that contains the main program and select Run As or Debug As. I appeared to have less trouble with it running the wrong project that way. The Debug perspective includes several new views that are 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.

Figure 1 The script will stop running and highlight a breakpoint

Next, step into the code by pressing F5 (or Run > Step Into ) a few times. As you step through the code,

the console window begins to display the results of the System.out commands. You can also watch the

variable i in the variables window on the top right of the Debug perspective or by hovering your cursor

over i in the code.

Figure 2 The variables window updates as the code is running

TIP: To resume running the code (but stopping at the breakpoint), press F8 or Run > Resume. Double-

click the breakpoint to remove it.

As your Eclipse projects become more complex, the debugger will be an invaluable tool for fixing bugs.

Let's go back to stepping through the code. You previously left the cursor at the call to System.out.println(). If you want to see the code for System.out.println(), you can click Step Into; otherwise, click Step Over to execute the System.out.println() method and start the next iteration of the for loop. The tabbed notebook below the editor includes a Console view. Program output appears here; if you made the earlier change to the variable msg, the line "Jello, world!" will appear. You can either continue to click Step Over until the loop terminates or, if you find this process tedious, click Step Return to immediately finish executing the say() method and return to the main() method. Or, just click the Resume button to let the program run to the end.

Conclusion

In order to get out of the debugger, do two things. Hit the red square to terminate the program. Click on the java tab in the upper right hand corner of the big window. One of Eclipse's most useful features is its integrated debugger. The ability to execute code interactively—setting breakpoints, executing code line by line, and inspecting the value of variables and expressions—is powerful tool for investigating and fixing problems with the code.