Eclipse Debugging Tutorial by Lars Vogel

Most people know Eclipse as an integrated development environment (IDE) for Java. Eclipse is created by an open source community and is used in several different areas, e.g. as IDE for Java or for Android or as a platform to develop Eclipse RCP applications, etc.. The usage of Eclipse as a Java development environment will be described in this tutorial.

Grab a Kaspersky coupon code from us and have the worlds top anti virus system at discounted rates

Create a Java project “de.vogella.debug.first” with the package “de.vogella.debug.first” and the following classes.

<pre>package de.vogella.debug.first;
public class Counter {
private int result=0;
public int getResult() {
return result;
}
public void count() {
for (int i = 0; i &lt; 100; i++) {
result += i +1;
}
}
}</pre>
<pre>package de.vogella.debug.first;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
Counter counter = new Counter();
counter.count();
System.out.println("We have counted " + counter.getResult());
}
}

To set breakpoints right click in the small left column in your source code editor and select toggle breakpoint. Or you can double click on this place.

You can debug your application with selecting your Java file which contains a main method, right click it and select Run -> Debug.

If you have not defined any breakpoints this will run your program as normal. To debug the program you need to define breakpoints.

If you start the debugger the first time Eclipse will asked you if you want to switch to the debug perspective. Answer “yes”, you should then see a perspective similar to the following.

You can use F5 / F6, F7 and F8 to step through your coding.

Table 1. Debugging Key bindings

Command Description
F5 Goes to the next step in your program. If the next step is a method / function this command will jump into the associated code.
F6 F6 will step over the call, e.g. it will call a method / function without entering the associated code.
F7 F7 will go to the caller of the method/ function. So this will leave the current code and go to the calling code.
F8 Use F8 to go to the next breakpoint. If no further breakpoint is encountered then the program will normally run.

You can of course use the ui to debug. The following displays the keybindings for the debug buttons.

The current stack is displayed in the “Debug” view.

The view “Variables” displays fields and local variables from the current stack.

Use the menu to display static variables.

Via the menu you can also customize the displayed columns, e.g. you can show the acutual and the declared type.

Another nice feature is the the “New Detail Formater” in which you can define how a variable is displayed. For example the toString method in our counter shows something meaningless, e.g. “de.vogella.debug.first.Counter@587c94”. Use right mouse click on the variable -> “New Details Formater”

Maintain the following code to get the output “0” (or whatever is field result holds at this point).

If you want to temporary de-activate all your breakpoints you can press the button “Skip all breakpoints” which is visible if you select the tab breakpoints.

If you press this button again the breakpoints will get activated again.

After setting a breakpoint you can select the properties of the breakpoint to for example use a condition to restrict when the breakpoint should get toggeled. In the properties you can for example restrict that the breakpoint should only be executed the 12 hit (Hit Count) or you can put in a conditional expression (which you can also use for logging if you want).

A watchpoint is a breakpoint at which is stop whenever a field read or changed. You can set a watchpoint through a double-click on the left side before the field declaration. Via the properties of the watchpoint you can define if the breakpoint should be hit during read access (Field Access) or during write access (Field Modification).

The application is stopped if the specified exception is thrown. To define an exception breakpoint click on the following icon.

You can define if you want to stop and caught and / or uncaught exceptions.

A method breakpoint is defined via double-click in the left border of the editor and the method head. You can define if you want to stop the program during method entry of after leaving the method.

A Class Load Breakpoint will stop when the class is loaded. Right-click on a class in the Outline View and choose “Toggle Class Load Breakpoint”

For every breakpoint you can define via the properties the hit count. If you use the hit count then the application is stop then the breakpoint is reached the number of times defined in the hit count.

Eclipse allows you to select any level (frame) in the call stack during debugging and set the JVM to restart to that point.

This allows you to rerun a part of your program. Be aware that variables which have been modified by the code which you reset remain modified. The drop to frame will return to the the code. Changes made by the code, e.g. to variables / databases will not be reset.

To use the feature select the level in your stack and press the highlighted button.

Details and Source : http://www.vogella.de/articles/EclipseDebugging/article.html

Leave a Reply

Your email address will not be published. Required fields are marked *