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.

Comments on "Top 10 Java Debugging Tips with Eclipse" Tutorial:

  1. Kapil says:

    Very useful Indeed!
    Thank you very much.

  2. srinivas says:

    thanx,
    joe

  3. sandeep says:

    Sir, keep writing such wonderfull tutorials.

  4. IDEA user says:

    Nice one.
    Can you write a IDEA version?

  5. askkuber says:

    thanks you for detail inforamation in one place

  6. saidulu says:

    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.

  7. vivek@cgvakindia.com says:

    Thanks Joe…

  8. chetan says:

    Very useful , every developer should know these things.

  9. Nagesh Kekan says:

    justt…..awesome

  10. padmanaban says:

    could u explain clearly about API and Package

  11. amit h says:

    Very well written

  12. Anonymous says:

    You are simply Superb

  13. krishnamurthy says:

    Super…..Jeo

  14. Anonymous says:

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

  15. G.Palanikumar says:

    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.

  16. Gootam says:

    Good work Joe.. Please keep it up.

  17. Prashant says:

    Thanks JoeSir,

    it,s very useful and easy to understand like it

  18. Satish says:

    Nice Job. Keep going

  19. Anonymous says:

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

    Thanks,
    Anandan R

  20. Hardik says:

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

  21. SANKAR says:

    Very use full information send me more information

    thank u
    Sankar svs

  22. java67 says:

    Great information, thank you

  23. mohan says:

    Nice post and Well Explained.Thank You

  24. Anonymous says:

    Getting more information. Please keep post such information. Thanks

  25. Anonymous says:

    Really Helpful…Thanks

  26. V Kishore Marisetty says:

    really use full, and simple

    Thank you very much…

  27. Anonymous says:

    serious gud one

  28. Murali says:

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

  29. Ram says:

    Really cool tips….

  30. ram says:

    Really good one…

  31. Kent says:

    nice article

  32. Mayur Kumar says:

    Hi Joe,

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

    keep the good work.

  33. Anonymous says:

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

  34. jcaleb says:

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

  35. S L N V Praveen says:

    superb Joe…

  36. Anonymous says:

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

  37. abs says:

    thanks! Just what I was looking for.

  38. Deepak says:

    Brilliant! Thanks a lot…

  39. dayang says:

    thanks

  40. SR says:

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

  41. J...?...V...? says:

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

  42. Anonymous says:

    very useful information at one place for software developers ..

  43. Anonymous says:

    Great collection

  44. Padmanaban says:

    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

  45. Joe says:

    @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.

  46. Anonymous says:

    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

  47. jegan says:

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

  48. Sikandar says:

    good info!!

  49. Anonymous says:

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

    kiran G.

  50. Anonymous says:

    thank you sir loved article alot

  51. Pitabasa Biswal says:

    Thanks a lot sir

  52. Amol patil says:

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

  53. jupiter says:

    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

  54. Babu says:

    Which is the ‘first best feature’ you love?

  55. kirti says:

    very useful thanks!

  56. SaratthM says:

    Thanks Joe; Very useful!

  57. pavan says:

    nice and useful article

  58. I Muyeed says:

    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.

  59. silambarasan says:

    Very nice blog

  60. S says:

    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.

  61. Shailaja says:

    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.

  62. Jay says:

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

  63. sivaram says:

    great tutorial. very useful

    thanks alot

  64. Kyle C says:

    awesome Thanks

  65. Mohammed Subhi says:

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

  66. Amateur says:

    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!

  67. Suriya says:

    Thanks for explaining it in very simple and effective way

  68. It’s very good tutorial,very useful….
    thanx Joe..

  69. venkat says:

    well concepts. really thanks …….

  70. Anonymous says:

    well documented

  71. Anonymous says:

    This is Great!

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

  72. Anonymous says:

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

  73. Anonymous says:

    Great, I ve learned new useful features

  74. pinky says:

    joe sir..
    awesome job… very useful

  75. […] you will blame me, saying you didn’t expect a so basic article from me. If you are looking for Eclipse debugging tips in general, you can refer to this earlier written super hit […]

  76. Venkatesh says:

    Thanks Joe!

  77. Nilesh says:

    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.

  78. yuvraj thorat says:

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

  79. kushi says:

    Thanks for sharing ur knowledge.. Appreciated!!

  80. Sachin says:

    Very concise and well written. Thanks.

  81. reshma says:

    nice…useful

  82. Edward says:

    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.

  83. santosh says:

    good sir your tutarials

  84. Ganesh says:

    Hi guys,

    I Debug a one program successfully.
    After that when i am doing debugging another program the error message has been displayed like source not found…

    please help me

  85. Jun says:

    Nice~

  86. ramesh says:

    Nice way of explanations

  87. George says:

    This is great. Thanks.

Comments are closed for this "Top 10 Java Debugging Tips with Eclipse" tutorial.

↑ Go to top