Eclipse Debugging


JAVA Debugging in Eclips

JAVA Debugging in Eclips

This article describes how to debug a Java application in Eclipse.

This article is based on Eclipse 4.2 (Eclipse Juno).


1.1. What is debugging?

Debugging allows you to run the program interactively and to watch the source code and the variables during this execution.

1.2. Debugging support in Eclipse

Eclipse allows to start a Java program in Debug mode. You can set breakpoints in your Java code at which the execution of the Java code will stop.

Eclipse has a special Debug Perspective which allow to control the execution process of your program and to investigate the state of the variables.

2. Prerequisites

The following assumes you know how 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 .

 

3. Debugging in Eclipse

3.1. Setting Breakpoints

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

Setting a breakpoint

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

Showing a defined breakpoint

3.2. Starting the Debugger

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

Start the debugger

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.

Switch to perspective

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.

Debug Shortcuts

3.3. Stack

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.

Showing the stack view

3.4. Evaluating variables

The Variables Ciew displays fields and local variables from the current stack. Please note you need to run the debugger to see the variables in this View.

Variables View

Use the drop-down menu to display static variables.

Drop-down menu for 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 Layout → Select Columns… → Type.

Showing the actual type of the variables

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

New Detail Formater Screenshot

For example to use the getResult() method for the display in the Variables View you could add the following code.

Detailer formater example

4. Advanced Debugging

The following section shows advanced options for debugging. It is not based on the example introduced previously.

4.1. Skip all breakpoints

If you want to temporary de-activate all your breakpoints you can press the Skip all breakpoints button. This button is visible, if you select the Breakpoints tab.

If you press this button again, your breakpoints will be reactivated.

De-activating all breakpoints

4.2. Breakpoint Properties

After setting a breakpoint you can select the properties of the breakpoint, via right-click → Breakpoint Properties. You can define a condition that restricts when the breakpoint will become active.

For example, specify that the breakpoint should only be active after it 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.

Breakpoint Properties
Breakpoint Properties

4.3. Watchpoint

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 during write access (Field Modification).

Watchpoint

4.4. Exception Breakpoint

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.

Exception Breakpoint

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

4.5. Method Breakpoint

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.

Method Breakpoint

4.6. Class Load Breakpoint

A Class Load Breakpoint will stop when the class is loaded.

To set a class load breakpoint, right-click on a class in the Outline View and choose “Toggle Class Load Breakpoint”.

Toogle class load breakpoint

4.7. Step Filter

You can define which packages should be skipped in debugging. This is for example useful if you use a framework for testing but don’t want to step into the test framework classes. You can define these packages via the Window →Preferences → Java → Debug → Step Filtering menu path.

4.8. Hit Count

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.

4.9. Drop to frame

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.

5. Exercise: Create Project for debugging

5.1. Create Project

For debugging we will create an example project. Create a Java project with the name de.vogella.combug.firstand add the package de.vogella.combug.first. Create the following classes.

package de.vogella.combug.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.combug.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());
  }
}

5.2. Debugging

Put a breakpoint in the Counter class. Follow the execution of the count method.

Define a Detailed Formater for Counter which uses the getResult method.

Delete your breakpoint and add a break point for class loading.

[COLLECTED FROM : http://www.vogella.com]

Advertisements

One thought on “Eclipse Debugging

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s