/
Advanced Java Debugging in Eclipse

Advanced Java Debugging in Eclipse

How To: Advanced Java Debugging in Eclipse

This wiki page lists out some additional tips/hints that can be useful for debugging Java applications using Eclipse. Also see a previous How To article describing how to debug a remote JBoss server from Eclipse. Please add your own hints/tips - I'm certainly always looking for new and better ways to debug my code.

Expressions View

Eclipse comes with a view called the Expressions view. This view is used to add Watch Expressions. A Watch Expression is simply a little bit of Java code that returns a value. This code is evaluated within the current debugger context, and the resulting value can be viewed. This sort of thing is helpful while stepping through code that modifies the state of some variable or class. The entries in the Expressions View will be re-evaluated each time the code is Stepped.

To open the Expressions View, choose Window->Show View->Expressions

Once the view is up, you can simply right-click in the view and choose Add Watch Expression.... In the resulting dialog, simply enter some Java code that will be evaluated within the current debugging context. You will have access to any variables that are currently in-scope.

Note that you can also add Watch Expressions to the view by highlighting some code in a Java editor, right-clicking and choosing Watch.

Detail Formatters

While debugging, it is obviously important to view the state of the variables that are currently in context. The Eclipse Variables View is used for this purpose. When clicking on a variable in the Variables View, Eclipse will either show the value (for simple types) or it will call toString() (for objects). If a particular object does not have an interesting implementation of toString(), then the default toString() defined in java.lang.Object will be invoked. This typically yields less than desirable results. Further, some implementations of toString() are meant for logging, rather than debugging. In these situations, Eclipse allows Detail Formatters to be used. A Detail Formatter is a bit of custom Java code that returns the value to be shown when clicking on a variable in the Variables View.

A good example is Exception. I often put breakpoints in catch blocks when things are going wrong. Generally what I want to see when debugging is the full stack trace of the Exception. This is not the default toString() behavior. Since I obviously cannot change the toString() implementation (found in java.lang.Throwable), I must use a Detail Formatter to see the full stack trace in the Variables view.

When sitting at a breakpoint, I can right-click on the Exception variable and choose New Detail Formatter. In the resulting dialog, I would change the Qualified type name to java.lang.Throwable and then paste in the following Java code:

java.io.StringWriter w = new java.io.StringWriter();
java.io.PrintWriter pw = new java.io.PrintWriter(w);
this.printStackTrace(pw);
return w.toString();

After I click OK, I can click on my Exception variable again after which I'll see the full stack trace from my Exception rather than just the message. In addition, since I changed the Qualified type name to make it as generic as possible, I will now see a full stack trace whenever I click on anything that derives from java.lang.Throwable.

Quick Inspect

While debugging my Java code, it is often the case that I wish to see the value of something that is not stored in a simple variable. For example, if I have the following Java code:

MyObject obj = createMyObject();
this.someMethod(obj.calculateTotal());

If I want to know the value that calculateTotal() will return before calling someMethod() then I have a few options. First, I could refactor my code to store the result in a variable, then pass the variable to someMethod(). Another option is to simply step into someMethod(). In most cases I don't want to do either of those things. So another alternative is the Eclipse Inspect feature. In my Java editor, I can highlight the obj.calculateTotal() code (only that bit of code). Then I can right-click and choose Inspect. The result is that Eclipse will evaluate the highlighted code and display the result in a quick pop-up dialog.