Top 10 Java Debugging Tips with Eclipse

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.

时间: 2024-12-28 11:40:15

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

Top 10 Mistakes Java Developers Make(转)

文章列出了Java开发者最常犯的是个错误. 1.将数组转换为ArrayList 为了将数组转换为ArrayList,开发者经常会这样做: ? 1 List<String> list = Arrays.asList(arr); Arrays.asList()会返回一个ArrayList,但这个ArrayList是Arrays的私有静态类,不是java.util.ArrayList.java.util.Arrays.ArrayList有set(), get(), contains()方法,但没有任

Top 10 questions about Java Collections--reference

reference from:http://www.programcreek.com/2013/09/top-10-questions-for-java-collections/ The following are the most popular questions of Java collections asked and discussed on Stackoverflow. Before you look at those questions, it's a good idea to s

Top 10 Mistakes Java Developers Make--reference

This list summarizes the top 10 mistakes that Java developers frequently make. #1. Convert Array to ArrayList To convert an array to an ArrayList, developers often do this: List<String> list = Arrays.asList(arr); Arrays.asList() will return an Array

Top 10 Methods for Java Arrays

作者:X Wang 出处:http://www.programcreek.com/2013/09/top-10-methods-for-java-arrays/ 转载文章,转载请注明作者和出处 The following are top 10 methods for Java Array. They are the most voted questions from stackoverflow. 0. Declare an array String[] aArray = new String[5

Top 10 Questions about Java Exceptions--reference

reference from:http://www.programcreek.com/2013/10/top-10-questions-about-java-exceptions/ This article summarizes the top 10 frequently asked questions about Java exceptions. 1. Checked vs. Unchecked In brief, checked exceptions must be explicitly c

Top 10 tough core Java interview questions answers programming

Tough core Java interview questions and answersWhat is tough core java interview question ? Why do people look for tough Java questions before going for interview? well I don't thing I need to answer these tough questions because its pretty natural t

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

这里列举了Java Array 的前十的方法.他们在stackoverflow最大投票的问题. The following are top 10 methods for Java Array. They are the most voted questions from stackoverflow. 0.声明一个数组 0. Declare an array String[] aArray = new String[5]; String[] bArray = {"a", "b&

开发Node.js应用最佳IDE Top 10(下)

在开发Node.js应用最佳IDE Top 10(上)一文中,我们为大家介绍了排名前5的开发Node.js应用最佳的IDE,有Cloud 9.Intellij IDEA.WebStorm.Komodo.Koding这五个最受欢迎的IDE.今天,我们将继续为大家揭晓余下的排名,一起来看看都有哪些IDE榜上有名吧! 6) Eclipse Eclipse是一个使用Java. PHP.C++等开发Web的基于云的流行IDE.您可以使用Node.js插件轻松地利用Eclipse IDE的nodeclips

TOP 10 ONLINE COMPILER

Top 10 Online Compilers +1338 Tweet Share106 Share Pin 444 Shares Online compilers are one type of tools that allows to compile the source code and execute it. Online compilers execute in a various programming language. We use different compilers to