


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 4
This page cannot be seen from the preview
Don't miss anything!



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