Java Debugging with Eclipse - Tutorial

1. Overview

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".

2. Prerequisites

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 .

3. Example Project for Debugging

For debugging we will create an example project. Create a Java project with the namede.vogella.combug.first and add the package de.vogella.combug.first. Also 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()); } }

4. Debug

4.1. Setting Breakpoints

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.

Setting a breakpoint

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

Showing a defined breakpoint

4.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

4.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

4.4. Variables

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.

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 the toString() 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

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

Detailer formater example

5. Advanced Debugging

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

5.1. Skip all breakpoints

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.

De-activating all breakpoints

5.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

5.3. Watchpoint

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

Watchpoint

5.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.

5.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

5.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

5.7. 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.

5.8. 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值