1.1. What is debugging?

Debugging allows you to run a program interactively while watching the source code and the variables during the execution.

breakpoint in the source code specifies where the execution of the program should stop during debugging. Once the program is stopped you can investigate variables, change their content, etc.

To stop the execution, if a field is read or modified, you can specify watchpoints.

 

Breakpoints and watchpoints are sometimes called stop points.

1.2. Debugging support in Eclipse

Eclipse allows you to start a Java program in Debug mode.

Eclipse provides a Debug perspective which gives you a pre-configured set of views. Eclipse allows you to control the execution flow via debug commands.

1.3. Setting Breakpoints

To define a breakpoint in your source code, right-click in the left margin in the Java 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.4. Starting the Debugger

To debug your application, select a Java file with a main method. Right-click on it and select Debug As ▸ Java Application.

 

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

If you have not defined any breakpoints, program as normally. To debug the program you need to define breakpoints. 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.

1.5. 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 allow use shortcut key to step through your coding. The meaning of these keys is explained in the following table.

Table 1. Debugging key bindings / shortcuts
Key Description

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 stepping into it in the debugger.

F7

F7 steps out 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 resume the execution of 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.6. Breakpoints view and deactivating breakpoints

The Breakpoints view allows you to delete and deactivate breakpoints and watchpoints. You can also 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 disable all breakpoints at the same time, 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.7. 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.

 

As of Eclipse 4.7 you also see the return statement of the last method call in the debugger.

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.8. Changing variable assignments in the debugger

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

1.9. Controlling the display of the variables with Detail Formatter

By default the Variables view uses the toString() method to determine how to display the 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. com.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.

 

2. Advanced Debugging

The following section shows more options you have for debugging.

2.1. Breakpoint properties

After setting a breakpoint you can select the properties of the breakpoint, via right-click ▸ Breakpoint Properties. Via the breakpoint properties you can define a condition that restricts the activation of this breakpoint.

You can for example specify that a breakpoint should only become active after it has reached 12 or more times via the Hit Countproperty.

You can also create a conditional expression. The execution of the program only stops at the breakpoint, if the condition evaluates to true. This mechanism can also be used for additional logging, as the code that specifies the condition is executed every time the program execution reaches that point.

The following screenshot depicts this setting.

2.2. 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 configure if the execution should stop during read access (Field Access) or during write access (Field Modification) or both.

2.3. Exception breakpoints

You can set breakpoints for thrown exceptions. To define an exception breakpoint click on the Add Java Exception Breakpoint button icon in the Breakpoints view toolbar.

You can configure, if the debugger should stop at caught or uncaught exceptions.

2.4. Method breakpoint

A method breakpoint is defined by double-clicking in the left margin of the editor next to the method header.

You can configure if you want to stop the program before entering or after leaving the method.

2.5. Breakpoints for loading classes

A class load breakpoint stops when the class is loaded.

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

Alternative you can double-click in the left border of the Java editor beside the class definition.

2.6. Step Filter

You can define that certain 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. These packages can be configured via the Window ▸ Preferences ▸ Java ▸ Debug ▸ Step Filtering menu path.

2.7. Hit Count

For every breakpoint you can specify a hit count in its properties. The application is stopped once the breakpoint has been reached the number of times defined in the hit count.

2.8. Remote debugging

Eclipse allows you to debug applications which runs on another Java virtual machine or even on another machine.

To enable remote debugging you need to start your Java application with certain flags, as demonstrated in the following code example.

java -Xdebug -Xnoagent \
-Djava.compiler=NONE \
-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005

In you Eclipse IDE you can enter the hostname and port to connect for debugging via the Run ▸ Debug Configuration…​ menu.

Here you can create a new debug configuration of the Remote Java Application type. This configuration allows you to enter the hostname and port for the connection as depicted in the following screenshot.

NOTE:Remote debugging requires that you have the source code of the application which is debugged available in your Eclipse IDE.

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

To use this feature, select a level in your stack and press the Drop to Frame button in the toolbar of the Debug view.

  Fields and external data may not be affected by the reset. For example if you write a entry to the database and afterward drop to a previous frame, this entry is still in the database.

The following screenshot depicts such a reset. If you restart your for loop, the field result is not set to its initial value and therefore the loop is not executed as without resetting the execution to a previous point.

3. Exercise: Create Project for debugging

3.1. Create Project

To practice debugging create a new Java project called de.vogella.combug.first. Also create the packagede.vogella.combug.first and 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());
}
}
 
 
 

Java Debugging with Eclipse - Tutorial的更多相关文章

  1. Top 10 Java Debugging Tips with Eclipse

    In this tutorial we will see about debugging java applications using Eclipse. Debugging helps us to ...

  2. 安装Java的IDE Eclipse时出现java.net.SocketException,出现错误Installer failed,show.log

    ERROR: org.eclipse.equinox.p2.transport.ecf code=1002 Unable to read repository at http://download.e ...

  3. java如何在eclipse编译时自动生成代码

    用eclipse写java代码,自动编译时,如何能够触发一个动作,这个动作是生成本项目的代码,并且编译完成后,自动生成的代码也编译好了, java编辑器中就可以做到对新生成的代码的自动提示? 不生成代 ...

  4. JAVA开发工具eclipse中@author怎么改

    1:JAVA开发工具eclipse中@author怎么改,开发的时候为了注明版权信息. 用eclipse开发工具默认的是系统用户,那么怎么修改呢 示例如图所示 首先打开Eclipse--->然后 ...

  5. ②---Java开发工具Eclipse安装配置

    Java开发工具Eclipse安装及配置 以下将为大家介绍Java开发工具Eclipse安装及配置. 一.下载Eclipse安装文件 正所谓工欲善其事必先利其器,我们在开发java语言过程中同样需要依 ...

  6. 转换基于Maven的Java项目支持Eclipse IDE

    在过去的教程中,使用 Maven 创建了一个Java项目,但是这个项目不能导入到Eclipse IDE中,因为它不是 Eclipse 风格的项目. 这里有一个指南,向您演示如何转换 Maven 生成 ...

  7. 使用Java EE 在eclipse 开发动态的Web工程(Java web项目)

    1.使用Java EE 在eclipse 开发动态的Web工程(Java web项目)1)开发开发选项切换到JavaEE2)可以在Windows->show view中找到package exp ...

  8. [C++] Solve "No source available for main()" error when debugging on Eclipse

    In Mac, the issue image: 1. A existing cmake project on disk 2. import this project into Eclipse. 3 ...

  9. Java 安装教程(Eclipse) + 汉化 + 简单创建java项目

    Java 安装教程(Eclipse) 要安装Java 要分两个步骤: 1.JDK的安装 2.Eclipse的安装 3.Eclipse汉化 4.Eclipse创建简单java项目 1和2的顺序不能颠倒, ...

随机推荐

  1. Gson 与 fastJson 在使用上的差异(fastJson的优点)

    一.android 常用的json解析方式 Android 开发上常用的json解析方式有:Gson, fastJson,jackson. 因为jackjson jar包会比较大点(700+k),Gs ...

  2. Android典型界面设计(8) ——ViewPager+PagerSlidingTabStrip实现双导航

    一.问题描述 PagerSlidingTabStrip是android开源项目,指示器控件.官网地址:https://github.com/astuetz/PagerSlidingTabStrip 该 ...

  3. Android中使用adb访问SQLite的方法

    (1)打开命令提示符,输入:adb,按回车,如果得到下面一大堆命令说明(如图 1),表示adb的配置是成功的,如果提示"不是内部或外部命令,也不是可运行的程序或批处理文件",那么需 ...

  4. Oracle 12c pdb的数据泵导入导出

    12c推出了可插拔数据库,在一个容器cdb中以多租户的形式同时存在多个数据库pdb.在为pdb做数据泵导入导出时和传统的数据库有少许不同.           1,需要为pdb添加tansnames ...

  5. N1, T1刷机记录

    硬件配置 N1和T1使用的是晶晨Amlogic方案的芯片, 配置明细分别如下, 都是现在盒子的主流配置 N1CPU: Amlogic S905, ARM Cortex-A53 四核 up to 2.0 ...

  6. top命令详析及排查问题使用演示

    1. top基本使用 top命令运行图 第一行:基本信息 第二行:任务信息 第三行:CPU使用情况 第四行:物理内存使用情况 buff/cache: buffers 和 cache 都是内存中存放的数 ...

  7. PHP —— 识别运算符实现逻辑比较

    最近遇到一个功能的开发,大致意思就是根据用户输入的条件,进行相关的比较操作.本来打算使用用户选择运算符的方式,但是后来结合项目实际,发现需要使用用户输入的自定义运算比较现实一点.大致意思就是: 1.用 ...

  8. 动软 生成 linq相关DAO

    第一步:新建自定义模板 <#@ template language="c#" HostSpecific="True" #> <#@ outpu ...

  9. 【Socket】关于socket长连接的心跳包

    TCP的socket本身就是长连接的,那么为什么还要心跳包呢? 在smack里有个30s发送一个空消息的线程,同样关于心跳包(keepalive) 据网络搜索到的资料解释如下 内网机器如果不主动向外发 ...

  10. Linux 下mysql的定时备份

    在实际项目中,数据库是要经常备份的,就是为了防止突发情况,前段时间,我的数据库就遭遇了入侵要支付B特比的,结果数据全没了,哎,还好当时只是个测试库,不过有了这次危机,也就开始意识到了这个问题了. 先写 ...