Debugging in Eclipse

1. Debugging in Eclipse

1.1. 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. Alternatively you can double-click on this position.

 

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

  

1.2. Starting the Debugger

To debug your application, select a Java file which can be executed, right-click on it and select Debug As → Java Application.

For Maven projects use: Debug As -> Maven build

 

After you have started the application once via the context menu, you can use the created launch configuration again via the Debug button in the the Eclipse toolbar.

 

 

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, Eclipse asks you if you want to switch to the Debug perspective once a stop point is reached. Answer Yes in the corresponding dialog.

 

 

Afterwards Eclipse opens this perspective, which looks similar to the following screenshot.

 

 

1.3. Controlling the program execution

Eclipse provides buttons in the toolbar for controlling the execution of the program you are debugging. Typically it is easier to use the corresponding keys to control this execution.

You can use the F5, F6, F7 and F8 key to step through your coding. The meaning of these keys is explained in the following table.

Table 1. Debugging Key bindings
KeyDescription| F5 | Executes the currently selected line and goes to the next line in your program. If the selected line is a method call the debugger steps into the associated code. |

F6

F6 steps over the call, i.e. it executes a method without entering it in the debugger.

F7

F7 goes to the caller of the currently executed method. This finishes the execution of the current method and returns to the caller of this method.

F8

F8 tells the Eclipse debugger to continue to execute the program code until is reaches the next breakpoint or watchpoint.

 

The following picture displays the buttons and their related keyboard shortcuts.

 

 

The call stack shows 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.

 

 

1.4. Breakpoint view and deactivating breakpoints

The Breakpoints view allows you to delete and deactivate stop points, i.e. breakpoints and watchpoints and to modify their properties.

To deactivate a breakpoint, remove the corresponding checkbox in the Breakpoints view. To delete it you can use the corresponding buttons in the view toolbar. These options are depicted in the following screenshot.

 

 

If you want to deactivate all your breakpoints you can press the Skip all breakpoints button. If you press it again, your breakpoints are reactivated. This button is highlighted in the following screenshot.

 

 

1.5. Evaluating variables in the debugger

The Variables view displays fields and local variables from the current executing 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 of the Variables view you can customize the displayed columns. For example, you can show the actual type of each variable declaration. For this select Layout → Select Columns... → Type.

 

 

1.6. Changing variable assignments in the debugger

The Variables view allows you to change the values assigned to your variable. This is depicted in the following screenshot.

 

 

1.7. Controlling the display of the variables with Detail Formatter

By default the Variables view uses the toString() method to determine the display of a variable.

You can define a Detail Formatter in which you can use Java code to define how a variable is displayed.

For example the toString() method in the Counter class may show meaningless information, e.g.de.vogella.combug.first.Counter@587c94. To make this output more readable you can right-click on the corresponding variable and select the New Detail Formater entry from the context menu.

 

 

Afterwards you can use a method of this class to determine the output. In this example the getResult() method of this class is used. This setup is depicted in the following screenshot.

 

Related pages