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.


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 added on 30/08/2012.

«

»

Comments on “Top 10 Java Debugging Tips with Eclipse”

  1. Kapil

    Very useful Indeed!
    Thank you very much.

    • S

      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.

    • Shailaja

      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.

  2. srinivas

    thanx,
    joe

  3. sandeep

    Sir, keep writing such wonderfull tutorials.

  4. IDEA user

    Nice one.
    Can you write a IDEA version?

  5. thanks you for detail inforamation in one place

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

    Thanks Joe…

  8. chetan

    Very useful , every developer should know these things.

  9. Nagesh Kekan

    justt…..awesome

  10. padmanaban

    could u explain clearly about API and Package

  11. amit h

    Very well written

  12. Anonymous

    You are simply Superb

  13. krishnamurthy

    Super…..Jeo

  14. Anonymous

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

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

    Good work Joe.. Please keep it up.

  17. Thanks JoeSir,

    it,s very useful and easy to understand like it

  18. Satish

    Nice Job. Keep going

  19. Anonymous

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

    Thanks,
    Anandan R

  20. Hardik

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

  21. SANKAR

    Very use full information send me more information

    thank u
    Sankar svs

  22. Great information, thank you

  23. Nice post and Well Explained.Thank You

  24. Anonymous

    Getting more information. Please keep post such information. Thanks

  25. Anonymous

    Really Helpful…Thanks

  26. V Kishore Marisetty

    really use full, and simple

    Thank you very much…

  27. Anonymous

    serious gud one

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

  29. Ram

    Really cool tips….

  30. ram

    Really good one…

  31. Kent

    nice article

  32. Mayur Kumar

    Hi Joe,

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

    keep the good work.

  33. Anonymous

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

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

  35. Anonymous

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

  36. abs

    thanks! Just what I was looking for.

  37. Deepak

    Brilliant! Thanks a lot…

  38. SR

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

  39. Anonymous

    maja aa gya bahi

  40. J...?...V...?

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

  41. Anonymous

    very useful information at one place for software developers ..

  42. Anonymous

    Great collection

  43. Padmanaban

    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

  44. Joe

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

  45. Anonymous

    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

  46. jegan

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

  47. Sikandar

    good info!!

  48. Anonymous

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

    kiran G.

  49. Anonymous

    thank you sir loved article alot

  50. Pitabasa Biswal

    Thanks a lot sir

  51. Amol patil

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

  52. jupiter

    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

  53. Babu

    Which is the ‘first best feature’ you love?

  54. kirti

    very useful thanks!

  55. SaratthM

    Thanks Joe; Very useful!

  56. pavan

    nice and useful article

  57. I Muyeed

    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.

  58. silambarasan

    Very nice blog

  59. Jay

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

  60. sivaram

    great tutorial. very useful

    thanks alot

  61. Kyle C

    awesome Thanks

  62. Mohammed Subhi

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

  63. Amateur

    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!

  64. Suriya

    Thanks for explaining it in very simple and effective way

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

  66. venkat

    well concepts. really thanks …….

  67. Anonymous

    well documented

  68. Anonymous

    This is Great!

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

  69. Anonymous

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

  70. Anonymous

    Great, I ve learned new useful features

  71. pinky

    joe sir..
    awesome job… very useful

  72. Venkatesh

    Thanks Joe!

  73. Nilesh

    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.

  74. yuvraj thorat

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

  75. kushi

    Thanks for sharing ur knowledge.. Appreciated!!

  76. Sachin

    Very concise and well written. Thanks.

  77. reshma

    nice…useful

  78. Edward

    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.

  79. santosh

    good sir your tutarials

  80. Ganesh

    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

Your Comment