Version 1.5
Copyright © 2009, 2010, 2011 Lars Vogel
09.12.2011
| Revision History | ||
|---|---|---|
| Revision 0.1 | 18.04.2009 | Lars Vogel |
| created | ||
| Revision 0.2 - 1.5 | 01.07.2009 - 09.12.2011 | Lars Vogel |
| bug fixes and enhancements | ||
Table of Contents
Debugging allows you to run the program interactively and to watch the source code and the variables during this execution.
A Java program can be started in "Debug mode". You can set breakpoints in your Java code at which the execution of the Java code will stop, if the Java program is executed in "Debug mode".
The following assumes you know know to develop simple standard Java programs. This article will focus on how to debug Java applications in Eclipse.
The installation and usage of Eclipse as Java IDE is described in Eclipse Java IDE Tutorial .
For debugging we will create an example project. Create a Java project with the name
de.vogella.debug.first and add
the package
de.vogella.debug.first. Also create
the following classes.
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 < 100; i++) { result += i + 1; } } }
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 margin in your source code editor and select "Toggle Breakpoint". Or you can double-click on this position.

For example in the following screenshot we set an breakpoint on
the
line
Counter counter = new Counter();.

To debug your application, select a Java file which contains a main method, right click on it and select → .

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 asks 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. The meaning of these keys is explained in the following table.
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, i.e. it will call a method / function without entering the associated code. |
| F7 | F7 will go to the caller of the method/ function. 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 the program will run normally. |
These commands are also available in the toolbar of Eclipse. The following picture displays the buttons and their related keyboard shortcuts.
The call stack show the parts of the program which are currently
executed and how they relate to each other. The current stack is
displayed in the
"Debug"
View.

The view "Variables" displays fields and local variables from
the
current stack. Please note you need to run the debugger to see the
variables in this
View.
Use the drop-down menu to display static variables.

Via the drop-down menu you can also customize the displayed columns. For example, you can show the actual type of each variable declaration. For this select → → .

Another nice feature is the "New Detail Formater" in which you
can
define how a variable is displayed. For example the
toString()
method in our
Counter
class
shows something meaningless, e.g.
de.vogella.debug.first.Counter@587c94. Right mouse on the
→ .

Add the following code to get the current value of result
variable in
the
Counter
class of this example.

The following section shows advanced options for debugging. It is not based on the example introduced previously.
If you want to temporary de-activate all your breakpoints you can press the button "Skip all breakpoints". This button is visible, if you select the "Breakpoints" tab.
If you press this button again, your breakpoints will be reactivated.
After setting a breakpoint you can select the properties of the breakpoint, via → . For example, you can define a condition that restricts when the breakpoint will become active.
For example, specify that the breakpoint should only be active after is has been reached 12 or more times (Hit Count).
Or, you can put in a conditional expression (which you can also use for logging). Execution will stop at the breakpoint, if the condition evaluates to true.


A watchpoint is a breakpoint set on a field. The debugger will stop whenever that field is read or changed.
You can set a watchpoint by double-clicking on the left margin, next to the field declaration. In the properties of a watchpoint you can define if the execution should stop during read access (Field Access) and / or write access (Field Modification).

You can also set breakpoints which are triggered when exceptioins
are
thrown. To define an exception breakpoint click on the "Add Java
Exception Breakpoint"
icon in the "Breakpoints"
View
toolbar.
You can define if you want to stop for caught and / or uncaught exceptions.
A method breakpoint is defined by double-clicking in the left margin of the editor next to the method header.
You can define if you want to stop the program before entering or 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 a hit count in it's properties. The application is stopped once 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 from that point.
This allows you to rerun a part of your program. Be aware that variables which have been modified by code that already run will remain modified.
Changes made to variables or external data, e.g. files, databases, will not be reset when you drop to a previous frame.
To use this feature, select a level in your stack and press the
"Drop
to Frame" button in the toolbar of the "Debug"
View.
For example you can restart your "for" loop. The field "result" will not be reseted in this case..

Before posting questions, please see the vogella FAQ. If you have questions or find an error in this article please use the www.vogella.de Google Group. I have created a short list how to create good questions which might also help you.
http://www.eclipse.org/articles/Article-Debugger/how-to.html How to develop your own debugger
Eclipse RCP Training (German) Eclipse RCP Training with Lars Vogel
Android Tutorial Introduction to Android Programming
GWT Tutorial Program in Java and compile to JavaScript and HTML
Eclipse RCP Tutorial Create native applications in Java
JUnit Tutorial Test your application
Git Tutorial Put everything you have under distributed version control system