Table of Contents
Debugging in Java is the process of interactively running a program such that one can see how the actors (variables and data structures) are changing their values as a line of code is being compiled. This is a common method to find out errors in your code. You can set an ending marker also known as a breakpoint in your code. The code will only be compiled until that point and the variables will be updated until the end of the breakpoint. The process of debugging is supported in Eclipse.
Setting a Breakpoint
Open the Java editor and navigate to the line where you want to set a breakpoint. Right-click on the left margin of the editor at that line and click on ‘Toggle Breakpoint’.
Starting the Debugger
After you have set a breakpoint in your code, it is now turn to switch on the debugger. Select the class with the main method in it. Go to the Menu Bar at the top of your screen and select Debug As > Java Application. If you have not selected a breakpoint in your code, the compiler will run in the Java perspective. However, if it finds an error in the code, you will receive a prompt asking you to switch to the Debugger perspective. If you click ‘Yes’ the debugger will open immediately and you will be automatically switched to the Debugger perspective.
The Breakpoints View shows you all the selected breakpoints and watchpoints. You can also edit or remove them from this space itself. If you want to remove a certain breakpoint, simply remove it from the list. Use the ‘Skip All Breakpoints’ option if you want Eclipse to run the code without the Debugger perspective.
After you run a program in the Debugger mode, a variable table will open. This will help you investigate the error in your code and assess how each variable was modified with every line. You have the flexibility to change variable assignments in the Debugger.
int mystery = (int)(Math.random()* playGame + 1;