Skip to content


5 Tips for Debugging Java Code in Eclipse

5 tips you can use while debugging your Java code in Eclipse.

  1. Logical Structure
    The logical structure is very handy for examining things like maps and other java collections classes, instead of showing the full detail of all the internal variables the view will only show the “logical” pieces. It is activated/deactivated from “Variables” view using logical structure toggle button.


  2. Conditional Debugging
    Once you know where an error occurs, you will want to see what the program is doing right before it crashes. One way to do this is to step through every statement in the program, one at a time, until you reach the point of concern. Sometimes it’s better to just run a section of code and stop execution at that point so you can examine data at that location. It’s possible to declare conditional breakpoints triggered whenever the value of an expression changes. In addition, code assist is available when typing in the conditional expression.

    Right click your breakpoint and select Breakpoint Properties… to set a condition. Check “Enable Condition” box and write your expression below it. You can define a “hit count” that stops your code after that count is reached if you want.

  3. Java Exception Breakpoint
    Use “Java Exception Breakpoint” feature to track your exceptions more easily. For example, your program might be throwing a NullPointerException or FileNotFoundException and you want to track it down. Rather than go to the line of code and set a breakpoint, you can specify that the debugger should always stop when the exception occurs.

  4. Step Filtering
    “Step Into” is one of the most used feature while debugging the source code. Sometimes, it takes us through several stack traces to reach the code we want to debug. In such cases, “Step with Filters” functionality can help us. It simply filters out the classes that we don’t want to step into.

    To specify which classes to filter out when “Step with Filters” is used, the preferences must be set. To activate the preferences panel choose Window > Preferences from the main window. Once there navigate to Java > Debug > Step Filtering and choose the classes/packages to be filtered out. When you step through the code, ensure that the “Use Step Filters” toggle button in Debug view is on.

  5. Expression Evaluation
    “Inspect” is a handy feature for evaluating your expressions. In the Debug perspective, select your expression, right click it and select “Inspect” from the menu to evaluate. You can do it by simply Ctrl+Shift+I key combination.

    “Display View” is another efficient helper in evaluating expressions. Activate it from Window > Show View > Display, write your expressions to evaluate and execute them by right clicking and selecting “Display” from the context menu in the Debug perspective or simply by Ctrl+Shift+D shortcut. You can use code assist (Ctrl+Space) to compose your expressions.

Happy debugging. :D

Posted in Eclipse, Java, Tips & Tricks.

Tagged with , , , , .


21 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Marco Bakera says

    Didn’t know the step filtering feature yet. Thanks for a hint on that. I will give a try. :)

  2. Paul Gregoire says

    Nice tips! Debugging in Eclipse can be a challenge at times for everyone, whether they be experienced engineers or noobs.

  3. Mustafa Azak says

    Logical Structure is great.I have been looking for it. Thanks, great work. Keep going:)

  4. elif yavuz says

    Gerçekten çok i?ime yarad?, büyüksün ACC!

  5. Maysonnave says

    Hi,

    About logical structure :

    1 – Is it a 3.3 or a 3.4 tip ?
    We have tested this on a 3.3.X platform and it only works if you watch a variable.

    2 – You should had that one can define more logical structures
    Preferences->Java->Debug->Logical Structures

    Thanks for this useful hint.

  6. Abdullah Cetin CAVDAR says

    I have tested them using Eclipse 3.4 and “Preferences->Java->Debug->Logical Structures” is nice. Thanks for your addition. :D

  7. Jean-Marc Vanel says

    I’d like to add a trace (log) point, that would output a trace line and continue.

  8. Subburaj says

    Very nice illustrations

  9. Fredrik Attebrant says

    “Trace points” can to some extent be done by entering code into the “condition” of a conditional breakpoint.

    First do some printing, then make sure to return false if you want the execution to continue.

    Example:

    System.out.println(” foo has size: ” foo.getSize());
    return false;

  10. John Solomon says

    Is it possible to get the current value of all the variables in jre without giving sysouts in the class files. like the one in eclipse debugger?

    • Abdullah Cetin CAVDAR says

      Hi John,
      You can use profilers. They show all the current processes, threads, memory statuses etc. I generally use VisualVM (a free tool bundled with JDK http://visualvm.java.net/). It’s very easy use and help you much.
      Take care…

  11. Replica Hublot Big Bang says

    I’m impressed, I have to say. Really not often do I encounter a blog that’s each educative and entertaining, and let me inform you, you could have hit the nail on the head. Your concept is excellent; the problem is something that not sufficient individuals are speaking intelligently about. I am very joyful that I stumbled throughout this in my search for one thing relating to this.

  12. vishal mohite says

    thanks for all the information. it is very useful for programming debug

    • Abdullah Cetin CAVDAR says

      You’re welcome. Happy coding. :)

      • RACKTIM says

        I tried everything you guys sugtesged and still no joy but finally found the reason for my set up hanging at 57% So for me I had to add these lines to my /etc/php5/con.d/xdebug.ini file:zend_extension=/usr/lib/php5/20090626/xdebug.soxdebug.remote_enable=Onxdebug.remote_host= localhost xdebug.remote_port=9000xdebug.remote_handler= dbgp I don’t know what in my system went curly to make this a requirement but hopefully it can help someone else some day!

  13. java67 says

    Great tips. Another good tip is stopping on main is new one which is only available on Eclipse Juno version , isn’t it ? but you can still stop any particular Thread by using conditional breakpoint as shown in this article Debugging Java program in Eclipse

  14. 0guzhan says

    Cool! Thank you… You should widen your content with some basic and advance fundamentals of debugging in eclipse.

  15. paste codes borderlands 2 says

    You’re so interesting! I don’t think I’ve truly read anything like that before.

    So wonderful to discover someone with some original thoughts on this subject.
    Really.. thanks for starting this up. This site is one thing that is required on the web, someone with a bit of originality!

Continuing the Discussion

  1. 5 Tips for Debugging Java Code in Eclipse « v1ruz blog linked to this post on September 17, 2008

    [...] read more | digg story Explore posts in the same categories: Uncategorized [...]



Some HTML is OK

or, reply to this post via trackback.