Here are the steps to do extensive debugging of java code using Eclipse IDE with watch variables, expression and Inspect.
Step1: Enable breakpoint
Set the breakpoint at the line of code or point of method entry from where you would like to start debugging the code. Right click on the left margin of the editor next to the line of code and a context menu pops up. Select toggle breakpoint in the context menu as shown below:
Figure1: Toggle Breakpoint
Step2: Configure the breakpoint to stop execution
To start debugging, the execution should stop at the breakpoint specified. For this click on breakpoint properties (refer Figure1) and do the following:
- Check Hit count
- Specify value as 1
- Select “Suspend thread” option
This will stop the execution when the program hits the breakpoint.
Step3: Switch to debug perspective
In Eclipse, select Window –>Open Perspective –> Debug as shown below:
Step4: Run in debug mode
Now run the program in debug mode. Select Run –> Debug
Now the program starts running in debug mode and you would see the state of the thread as “running”
When the program hits the breakpoint the state of the thread changes from “running” to “suspended”
Step5: Debugging the code with Expressions \Watch variables \ Inspect
Now the code stops at the breakpoint. You could use the watch variables / expression to monitor the current value of the debug variable.
Suppose you set the breakpoint at the variable named “counter” and the program stopped at counter variable as shown below:
Add the expression with the variable name “counter” which allows you to monitor the value of the variable as you execute the program:
You could also right click on the counter variable and select “Inspect” from the context menu
If it is a method then, select the method name and click on “Step Into Selection“. This will allow you to monitor or debug the method execution line by line.
Step6: Use F6 key to step into the method
To “step into” the next executable line of code in the current method, press the “F6 Key”. This will pass the program control from the current line to the next executable line of code.