Top Ten Java debugging Tips with Eclipse

Source: Internet
Author: User

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

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

    • 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 would be created. In debug perspective, ' breakpoints ' view would list the breakpoint created. We can add a Boolean condition to it. That is, the breakpoint would be activated and execution would hold only if the Boolean condition is met otherwise this Brea Kpoint'll be skipped.

2. Exception Breakpoint

In breakpoints view there is a button labeled as J! We can use this 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 u Sing this.

3. Watch Point

This was one nice feature I love. When a chosen attribute was accessed or modified program execution would halt and allow to debug. Select a class variable in Outline view and from its context menu select Toggle watchpoint. This would create a watch point for that attribute and it'll 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 would show the value. We can also add a permanent watch on an expression/variable which would be shown in Expressions view when Debug was 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 the Configuration we can enable a check box that says Stop in main. If enabled when we debug a Java program this 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 configuratio N dialog box.

8. Drop to Frame

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 won't be reset. Choose the stack level which your want to go back and restart debug from there and click the drop to frame button from Debu G toolbar. Eclipse is cool!

9. Step Filter

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

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 have a method call the control would go into the first line of th E 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 L Ine.
    • F7–step Return:when done from inside a method the control would move to the calling line from where the current method I s invoked.
    • F8–move to next breakpoint.

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

Top ten Java debugging Tips with Eclipse

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.