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. cocos2dx系列笔记(2)- windows环境配置后续之 Android环境配置

    续上篇 对于想用cocos2dx来开发Android游戏的人来说,最痛苦的莫过于配置Android环境和之后的奇奇怪怪的编译失败问题.这是经历了多次成功与失败之后,血与泪的经验包,大家请收好.如果你有 ...

  2. 菜鸟日记之JSP二 内置对象的理解

    ·最近学习JSP了,对编程和网络又有了一些理解.无论是现实中人与人的交流,还是网络世界的接触,都是在相互表达自己的意思让别人知道,并理解对方的信息.然后我们知道的事情不断的变多,会又交杂出新的内容,不 ...

  3. [学习笔记]设计模式之Proxy

    为方便读者,本文已添加至索引: 设计模式 学习笔记索引 写在前面 “魔镜啊魔镜,谁是这个世界上最美丽的人?” 每到晚上,女王都会问魔镜相同的问题(见Decorator模式).这是她还曾身为女巫时留下的 ...

  4. jQuery 个人随笔

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. 互联网 免费的WebService接口

    winform开发暂告于段落,最近再用webservice写接口,接下来的一段时间应该偏向于此方向. (转)一批的免费webservice接口,没有技术含量,只是写在这里做个记忆 股票行情数据 WEB ...

  6. 或许有一两点你不知的C语言特性

    关键字篇 volatile关键字 鲜为人知的关键字之一volatile,表示变量是'易变的',之所以会有这个关键字,主要是消除编译优化带来的一些问题,看下面的代码 ; int b = a; int c ...

  7. 帝国cms 列表页分页样式修改美化【1】

    [1]自己修改帝国cms默认的分页样式(css),这样做的好处是你不用去改动帝国的核心文件,方便以后升级. [2]自己动手去修改帝国的分页(php+css),帝国的分页在e>class>下 ...

  8. Sharepoint 问题集锦 - 外部列表(external list) - 读取当前用户上下文或用户名作为筛选参数

    在创建外部列表过程中,往往需要添加筛选参数,而较多开发用户,会关心如何在外部列表中,只显示当前用户相关的行.本例子中,我们以任务数据表来做例子,看看如何实现这个需求. 1)数据表tbl_task: t ...

  9. EF自动生成的(T4模板) 关联属性元数据修改

    为了实现 T4模板关联属性 不要序列化的问题 就是要在具体的 关联属性上面添加一个元数据 这里利用以前的 Newtonsoft.Json 这个框架为例 效果应该为 就是要在关联属性上面添加元数据  [ ...

  10. 打log

    如果项目上过线的话,那你一定知道Log是多么重要. 为什么说Log重要呢?因为上线项目不允许你调试,你只能通过Log来分析问题.这时打一手好Log的重要性绝不亚于写一手好代码.项目出问题时,你要能拿出 ...