Monday 29 September 2014

Debugging JAVA code in Eclipse

DEBUGGING -
Debugging allows a program to run line by line interactively by watching the source code during execution.
Here, we need to give breakpoints in the source code which specifies where to stop the program execution.
Once, the program is stopped you can press F6 for executing the code line by line or F8 if you want the code to run normally.
F6 - Moves the control to next line. If there is a method call in the current line, it executes the method call internally and just moves the control to next line.
F8 - F8 tells the Eclipse debugger to resume the execution of the program code until it reaches the next Breakpoint.

DEBUGGING SUPPORT IN ECLIPSE -
Eclipse allows you to start a Java program in Debug mode.
Eclipse has a special Debug perspective which gives you a configured set of views. In this perspective you control the execution process of your program and can investigate the state of the variables.

How to debug the Java code in Eclipse -

Click on the debug icon on the top bar or right click on the Java class and click on Yes, to go to the debug perspective
OR
Click on Open Perspective icon on the top right corner, select debug then click OK, to open the debug perspective or if debug option is not visible there then click on others and select debug option.



STEPS FOR DEBUGGING A JAVA CODE IN ECLIPSE

Setting Breakpoints-
To set breakpoints in your source code right-click in the small left margin in your source code editor and select Toggle Breakpoint or you can double-click on this position.
 

For removing the set Breakpoint, double click on it or right click -> click on toggle Breakpoint.

Going to the debug perspective -
Click on the debug icon on the top bar or right click on the Java class or simply select debug option from Open Perspective icon present in top bar.







Confirm Perspective Switch popup will come, click on YES to move the debugger perspective
 

Afterwards Eclipse opens this debug perspective



Here, in the above screenshot shows the program execution stops at the line where breakpoint is given.

Controlling the program execution -
Here are some keys we need to control the execution of program in debug perspective:
F6 - Moves the control to next line. If there is a method call in the current line, it executes the method call internally and just moves the control to next line.
F8 - F8 tells the Eclipse debugger to resume the execution of the program code until is reaches the next Breakpoint.

Evaluating Variables in debugger -
 
The Variables view tab displays fields and local variables from the current executing stack,you need to run the debugger to see the variables in this view tab.
 
This is the Breakpoints view tab, which shows all the Breakpoint set in all different classes.


Results in debugger -
 

Here in this console tab , the results gets printed one by one.

To Move back from debug perspective to Java perspective -
To move back to Java perspective from debug perspective, click on open perspective icon on the top bar -> select Java -> click on OK .















1 comment: