How to Debug with Eclipse
Set Breakpoints: To set a breakpoint, select one line of code first, then move mouse to the left most area of that line(as shown in below picture), either double click or right click then select “Toggle Breakpoint” in the popup list, a small blue...
Step-by-Step Guide
-
Step 1: Set Breakpoints: To set a breakpoint
Now we'll add the variables into watch box to see if the program will run as we expected.
To add a variable into watch box, put the cursor on it, right click, then in the popup list select “Watch”. ,, To see if the function add() will work as we expected, we'll step into it.
To do so, just Press F5, or on tools panel, press the "Step Into" icon in the tools panel, or in main menu, select “Step Into” in the drop-down list of the item “Run”.
The program will run into function add() and stop on the first executable code. , The program will return from function add() to main() and stop on the same line when it left previously. ,, We must use Step Over instead of Step Into because we don't have the source code of the function println(). , From main menu “Windows”, select “Preference”, then follow the numbers sequentially in below picture: , That means if it is enabled, every time when a program starts to run, it will stop on the first executable code in main() so that the code can be run manually.
To enable “Stop in main”, right click on the project name in project explorer window, select “property” to bring out the “properties for xxx”(xxx is the project name) dialog box, then follow the steps labelled numbers sequentially. -
Step 2: select one line of code first
-
Step 3: then move mouse to the left most area of that line(as shown in below picture)
-
Step 4: either double click or right click then select “Toggle Breakpoint” in the popup list
-
Step 5: a small blue ball will appear
-
Step 6: that means a breakpoint has been set successfully.
-
Step 7: Start the program in debug mode: We have three ways to do so: 1> Press F11; 2> Click item “Run” in main menu then select “Debug” in the drop list; 3> Click the bug icon on the tools panel(as below picture shows) then select “Debug As Java Application”.
-
Step 8: Add variables to watch box for examination: We can see the program now is stopping at the line on which we've set a breakpoint.
-
Step 9: Check the variable values in the watch box: Now we can see the value of num1 and num2 are as expected
-
Step 10: but sum is still 0.0
-
Step 11: because the program hasn't run the code that will update sum’s value.
-
Step 12: Step Into: Run into the function: Now we'll use function add() to calculate the sum.
-
Step 13: Return from the function: Run the code by pressing F6
-
Step 14: or pressing the "Step Over" icon in the tools panel
-
Step 15: or in main menu
-
Step 16: select “Step Over” in the drop-down list of the item “Run”.
-
Step 17: Check the return value from function: Run the program with Step Over
-
Step 18: the value of sum will be changed to 9.0.
-
Step 19: Print the result: Run the program with Step Over.
-
Step 20: Set debug filter: To avoid stepping Into the functions without source code
-
Step 21: we'll have to modify some configurations to tell debugger not to step into those functions even step into command is used.
-
Step 22: Stop in main: There is another way to stop the execution of a program for debug purpose--Stop in main.
Detailed Guide
Now we'll add the variables into watch box to see if the program will run as we expected.
To add a variable into watch box, put the cursor on it, right click, then in the popup list select “Watch”. ,, To see if the function add() will work as we expected, we'll step into it.
To do so, just Press F5, or on tools panel, press the "Step Into" icon in the tools panel, or in main menu, select “Step Into” in the drop-down list of the item “Run”.
The program will run into function add() and stop on the first executable code. , The program will return from function add() to main() and stop on the same line when it left previously. ,, We must use Step Over instead of Step Into because we don't have the source code of the function println(). , From main menu “Windows”, select “Preference”, then follow the numbers sequentially in below picture: , That means if it is enabled, every time when a program starts to run, it will stop on the first executable code in main() so that the code can be run manually.
To enable “Stop in main”, right click on the project name in project explorer window, select “property” to bring out the “properties for xxx”(xxx is the project name) dialog box, then follow the steps labelled numbers sequentially.
About the Author
Mary Thomas
Mary Thomas specializes in educational content and has been creating helpful content for over 1 years. Mary is committed to helping readers learn new skills and improve their lives.
Rate This Guide
How helpful was this guide? Click to rate: