In this tutorial we will see about debugging java applications using Eclipse. Debugging helps us to identify and fix defects in the application. We will focus on run-time issues and not compile time errors. There are command line debuggers like gdb available. In this tutorial we will focus on GUI based debugger and we take our favourite IDE Eclipse to run through the tutorial. Though we say Eclipse, the points are mostly generic and is suitable for debugging using most of the IDEs like NetBeans too.

Before going through this tutorial, I recommend you to have a look at Eclipse shortcuts and it will really help. My Eclipse version is Juno as of writing this tutorial.

  • Do not use System.out.println as a tool to debug.
  • Enable detailed log level of all the components involved.
  • Use a log analyzer to read logs.

1. Conditional Breakpoint

Hope we know how to add a breakpoint. If not, just click on the left pane (just before the line number) and a breakpoint will be created. In debug perspective, ‘Breakpoints’ view will list the breakpoint created. We can add a boolean condition to it. That is, the breakpoint will be activated and execution will hold only if the boolean condition is met otherwise this breakpoint will be skipped.

2. Exception Breakpoint

In Breakpoints view there is a button labeled as J! We can use that button to add a java exception based breakpoint. For example we want the program to halt and allow to debug when a NullPointerException is thrown we can add a breakpoint using this.

3. Watch Point

This is one nice feature I love. When a chosen attribute is accessed or modified program execution will halt and allow to debug. Select a class variable in Outline view and from its context menu select Toggle Watchpoint. This will create a watch point for that attribute and it will be listed in Breakpoints view.

 

4. Evaluation (Display or Inspect or Watch)

Ctrl+Shift+d or Ctrl+Shift+i on a selected variable or expression
will show the value. We can also add a permanent watch on an
expression/variable which will be shown in Expressions view when debug
is on.

5. Change Variable Values

We can change the value of a variable on the fly during debug. Choose
a variable and go to Variables view and select the value, type and
enter.

6. Stop in Main

In Run/Debug Settings, Edit Configuration we can enable a check box
that says Stop in main. If enabled when we debug a java program that
launches with a main method, the execution halts at first line of main
method.

7. Environment Variables

Instead of going to System properties to add an environment variable,
we can conveniently add it through Edit Configuration dialog box.

8. Drop to Frame

This is the second best feature I love. We can just return the
control to any frame in the call stack during debug. Changes made to
variables will not be reset. Choose the stack level which you want to go
back and restart debug from there and click the drop to frame button
from debug toolbar. Eclipse is cool!

9. Step Filter

When we Step Into (F5) a method we may go into external libraries
(like java) and we may not need it. We can add a filter in preferences
and exclude packages.

10. Step Into, Over and Return

I kept this as the last point as this is the first thing to learn in debugging :-)

  • F5 – Step Into: moves to next step and if the current line has a
    method call the control will go into the first line of the called
    method.
  • F6 – Step Over: moves the control to next line. If there is a method
    call in the current line, it executes the method call internally and
    just moves the control to next line.
  • F7 – Step Return: When done from inside a method the control will
    move to the calling line from where the current method is invoked.
  • F8 – Move to next breakpoint.

This Core Java tutorial was added on 30/08/2012.

Top 10 Java Debugging Tips with Eclipse的更多相关文章

  1. Top 10 Methods for Java Arrays

    作者:X Wang 出处:http://www.programcreek.com/2013/09/top-10-methods-for-java-arrays/ 转载文章,转载请注明作者和出处 The ...

  2. Top 10 Questions about Java Exceptions--reference

    reference from:http://www.programcreek.com/2013/10/top-10-questions-about-java-exceptions/ This arti ...

  3. Top 10 Mistakes Java Developers Make--reference

    This list summarizes the top 10 mistakes that Java developers frequently make. #1. Convert Array to ...

  4. Top 10 Mistakes Java Developers Make(转)

    文章列出了Java开发者最常犯的是个错误. 1.将数组转换为ArrayList 为了将数组转换为ArrayList,开发者经常会这样做: ? 1 List<String> list = A ...

  5. Java Debugging with Eclipse - Tutorial

    1.1. What is debugging? Debugging allows you to run a program interactively while watching the sourc ...

  6. 【翻译】Java Array的排名前十方法(Top 10 Methods for Java Arrays)

    这里列举了Java Array 的前十的方法.他们在stackoverflow最大投票的问题. The following are top 10 methods for Java Array. The ...

  7. TOP 10 ONLINE COMPILER

    Top 10 Online Compilers +1338 Tweet Share106 Share Pin 444 Shares Online compilers are one type of t ...

  8. TOP 10开源的推荐系统简介

    最近这两年推荐系统特别火,本文搜集整理了一些比较好的开源推荐系统,即有轻量级的适用于做研究的SVDFeature.LibMF.LibFM等,也有重量级的适用于工业系统的 Mahout.Oryx.Eas ...

  9. Top 10 Algorithms for Coding Interview--reference

    By X Wang Update History:Web Version latest update: 4/6/2014PDF Version latest update: 1/16/2014 The ...

随机推荐

  1. C# 控制台窗口的显示与隐藏

    1. 定义一个Consolse帮助类,如下: /// <summary> /// 控制台帮助类 /// </summary> public static class Conso ...

  2. 配置wamp开发环境【1】

    新手在PHP网站建设时,会使用使用PHP的集成开发环境,这样利于开发和理解!但是做为一个网站开发人员,会独立的配置开发环境这是必须的……因为集成的环境毕竟是固定的,不利于自己的开发.好,废话少说咱现在 ...

  3. 导出文本、表格、图像到PDF格式文件中(学习整理)

    1.测试例子: 需要导入的外部jar包: 相关API http://www.coderanch.com/how-to/javadoc/itext-2.1.7/com/lowagie/text/pack ...

  4. 数据库(学习整理)----4--Oracle数据查询(基础点1)

    其他: 计算机中的内存是线性的,一维. length('')计算字符的个数,而不是字节的个数 Oracle中的日期类型和数值类型的数据可以做运算符(>,=,<,<>)比较 如果 ...

  5. 读书笔记之深入分析Java Web技术内幕

    章节: 1 B/SB/S的优点: 客户端使用统一的浏览器(Browser) ,浏览器的统一带来了操作的统一,无论使用什么服务,因为浏览器是相同的,所以操作类似.客户使用简单了.服务端开发简化; 使用统 ...

  6. 祭奠一年前写 Direct2D demo

    一年前, 用Direct2D实现了不怎么样的UI库. 用不怎么样的UI库实现了这个Demo. 当时放进某群共享, 以此装逼. 今天无意中翻出来, 解压, 什么都没变, 还是压缩前的模样. 不经意看见被 ...

  7. MySQL设置

    在MySQL的使用中很容易出现乱码的情况. 实际上在MySQL中有个地方表明了系统中所用到的所有的字符集. 例如: 从中可以看出,对于server和database的默认字符集都是latin1,这样很 ...

  8. 入门6:PHP 语法基础——循环

    一.for循环 for($i=0;$i<10;$i++){ echo "Hello".$i."</br>"; } 二.while循环 $i = ...

  9. 关于【键鼠<局域网>共享软件:synergy】install

    Installation 另外,本人在centos6.5环境下作为server运行时,遇到一个问题,synergy1.5随着系统升级居然变成了1.3X,所以如果遇到类似问题,请您先用 rpm -qa ...

  10. pip assert_source_matches_version(self)版本验证报错Source in %s has version %s, which satisfies requirement %s的解决方式

    在win8.1下为了安装flask模块,开始安装pip,结果发生了上篇博客里面的错误ntpath join(path, *paths) 发生UnicodeDecodeError.解决之后继续发现版本验 ...