Top 10 Java Debugging Tips with Eclipse

30/08/2012

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.


Ads by Google

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 posted on 30/08/2012.
Ads by Google

83 comments on “Top 10 Java Debugging Tips with Eclipse

    • Sir, i want you to post on remote debugging with eclipse, have been trying hard to find simple way of doing.Hope,you can really explain in better way.

    • Sir, i want you to post on remote debugging with eclipse, have been trying hard to find simple way of doing.Hope,you can really explain in better way.

  1. Sir, keep writing such wonderfull tutorials.

  2. Nice one.
    Can you write a IDEA version?

  3. This is what looking what long time, it’s really very good tutorial..

    Please explain servlet and jsp’s debbuging using live example as above.

  4. Very useful , every developer should know these things.

  5. could u explain clearly about API and Package

  6. thanks joe…very well written.. your passion helps us..

  7. Respected Sir,

    First I Thank you so much sir your website very useful to me.
    I am asking my doubts but i don’t know how to tell to you. Q1)How to input more than value at a time?. please give me example.

  8. Very useful information.. Nice keep send more information’s

    Thanks,
    Anandan R

  9. Awesome , Whenever I read your articles I really learn new thing, great…

  10. Very use full information send me more information

    thank u
    Sankar svs

  11. Getting more information. Please keep post such information. Thanks

  12. really use full, and simple

    Thank you very much…

  13. very interesting post. As a developer should know these techniques of debugging.

  14. Hi Joe,

    Thanks for your valuable input on the Eclipse IDE.
    It is really good.

    keep the good work.

  15. Useful tips.
    Have you ever tried debugging xqueries in eclipse?
    Please write something if you know something related.

  16. thanks man…. i dont know 8 out of 10 you posts. must catch up on my eclipse skills

  17. great work .. thanks for the much needed help in eclipse debugging

  18. Thank You Joe. Very useful, concise and to the point.

  19. Nice one, i liked it so much…..keep writing…

  20. very useful information at one place for software developers ..

  21. Hi Joe,
    I was trying to launch Eclipse Juno in my laptop which is installed with jdk1.7.0_01 with update 7. But Eclipse is getting terminated every time with the error message that failed to load JNI Shared library from the source. I dont understand that message and I have checked my PATH and CLASSPATH env variables and are fine. More over I am running my Netbeans IDE it is not complaining with the same error and also I am able to run java programme from the command prompt. This problem is only with eclipse. please guide me to settle this problem

    Thanks

  22. @Padmanaban,

    if you can share the exact error trace, I can attempt to solve it.

    Recently with Juno I face some similar error message. It was the first time when I was setting up Juno. Then I doubted about the downloaded zip file and I removed the whole setup, downloaded it fresh again and it worked nicely.

    My guess is some files may be missing or corrupted.

  23. You are doing an awesome job here!!… I think your site is one of the best ones to deliver a clear knowledge of Java which is really helpful!!!.. Keep up the good work

  24. you are super in explaining.. really very good articles all of them ..

  25. Hi Joe,
    your site is too good to read and it is very useful article for java guys.

    kiran G.

  26. Thank you very much
    Very good Info..
    may god bless you with whatever you want.. :)

  27. Congrats for the article.

    One question. You say “Use a log analyzer to read logs”. Could you please give an example of such a log analyser?
    I use basic Unix commands to go through the logs. I would be interested in taking a look at such a log analyser. What would the key features be?

    Cheers

  28. Which is the ‘first best feature’ you love?

  29. Look of ur website/blog is very attractive, I wish all the success. I got lot of in depth information from this about java.Thanks, I wish u all the success.

  30. Good Website….
    could you help me…..
    how to edit falsh template…..
    please help me out…..

  31. great tutorial. very useful

    thanks alot

  32. when I use Expression to variable inside object I face the error (error during the evaluation)

  33. Woah! I’m really loving the template/theme of this website. It’s simple, yet effective.
    A lot of times it’s hard to get that “perfect balance” between user friendliness and visual appearance. I must say that you’ve done
    a very good job with this. Additionally, the blog loads extremely quick for me on Chrome.
    Excellent Blog!

  34. Thanks for explaining it in very simple and effective way

  35. This is Great!

    display is also a great debugging tool in eclipse that you should talk about.

  36. Great, I ve learned new useful features (4+5+9)

  37. Great, I ve learned new useful features

  38. Pingback: Java Remote Debug with Eclipse

  39. Hi ,
    Nice artical. Joe actuallu i am working on one tool where i can’t use eclipse or any advanced tool for debugging. Only solution is we can debug by entering System.out.println() in a code. actuallly i want to know that is there any tool which add this System.out.println automatically in java file for every posiible variables . Waiting for your replay. Thank you in advance.

  40. Very nice explaination, keep writing….Hurray!!!!!!!!

  41. Thanks for sharing ur knowledge.. Appreciated!!

  42. Sir,

    Could you tell me how to add multi Conditional Breakpoint. I tried to do something like: nName.equals(“A”) && mAge == 12
    But it didn’t work.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>