Vous êtes sur la page 1sur 5

Java debugging tutorial - 10 tips on debugging in java with example

Javarevisited

Blog about Java programming language, FIX Protocol, Tibco Rendezvous and related Java technology stack.

WEDNESDAY, JULY 6, 2011

Best of Javarevisited

Java debugging tutorial - 10 tips on debugging in java with example

How Android works, Introduction for Java


Programmers
Difference between Java and Scala Programming

How to debug java program in Eclipse


Debugging is a must have skill for any java developer. Having ability to debug java program enables to
find you any subtle bug which is not visible during code review or comes when a particular condition offer,
This becomes even more important if you are working in high frequency trading or electronic trading
system project where time to fix a bug is very less and bug usually comes on production environment and
doesn't appear in your Windows XP machine. in my experience debugging java application also helps you
understand flow of java program. In this java tutorial we will see how to debug a java program, setting up
remote debugging in java and some java debugging tips on Eclipse and Netbeans IDE. Its also good to
know various java debug tool available and how java debugger or jdb works but its not mandatory for
doing debugging in Java. To start java debugging you just needs your project to be configured in a
modern IDE like eclipse and Netbeans and you are ready to debug java program.

Top 5 Java Programming Books for Developers


Top 10 JDBC Best Practices for Java programmers
Tips and Best practices to avoid
NullPointerException in Java
10 Object Oriented Design Principles Java
Programmer Should Know
10 HotSpot JVM Options, Every Java Programmer
Should Know

Subscribe To This Blog Free


Posts

Java debugging tools


I mostly used Eclipse IDE and Netbeans IDE for java development and these IDE have great
support for java debugging. They allow you to set various breakpoints like line breakpoint,
conditional breakpoints or exception breakpoint. I prefer Eclipse over netbeans because of its
seamless integration with remote debugging because most of the time your application will run
on Linux machine and you might not have local version running on your machine, in such
scenario remote debugging is extremely useful. You can check how to setup java remote debugging in
eclipse for step by step guide on setting remote debugging in eclipses. Apart from Eclipse and Netbeans IDE
you can also use Java debugger jdb which is a simple command line based java debugger and based on
java platform debugging architecture and can be used to debug java program locally or remotely.


Comments
Follow Us
Follow @javinpaul

Java debug options


If you are not using any IDE for java debugging locally you need to provide java debug option while starting
your program. You need to provide java debug option also if you are setting up remote debugging session
or using jdb for java debugging. Following are the two java debugging option which needs to be provided to
java program:
Debug Options

Purpose

Xdebug

Used to run java program in debug


mode

Xrunjdwp:transport=dt_socket,server=y,suspend=n

Loads in Process debugging libraries


and specifies the kind of connection to
be made.

Followers
Subscribe by email:
Subscribe

Suspend=y and n is quite useful for debugging from start or debugging at any point.

Using jdb to debug java application


1) Start your java program with two options provided above for example, below command will start
StockTrading java program in debug mode.
% java -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n
StockTrading

By Javin Paul
Loading

Blog Archive

2014
(
76
)

2013
(
136
)

2012
(
217
)

2011
(
145
)

December
(
28
)

November
(
14
)

After starting your java application in debug mode you can attach java debugger "jdb" to the VM with the
following command:

October
(
14
)

% jdb -attach 8000

August
(
11
)

September
(
22
)

July
(
7
)
You can check the jdb manual page for complete detail on how to do java debugging with jdb.

Java remote debugging with eclipse


This is another cool feature of eclipse which allows you to connect your java application running on remote
host and do remote debugging. You just need to start your java application with the java debug option
discussed above and then connect your application from eclipse into specified port. You can check below
link for step by step guide on java remote debugging with eclipse.

How to use Java 1. 7 Multiple Catch Block with


exa...

Top 15 Java Multithreading, Concurrency


Interview ...

Difference between ClassNotFoundException vs


NoCla...

Why multiple inheritances are not supported in


Jav...

Debugging java application in Eclipse and Netbeans

String vs StringBuffer vs StringBuilder in Java

Debugging java application locally on any IDE like Eclipse or Netbeans its very simple, just select the
project and click debug or use debug shortcut provided by IDE. You can also debug a single java class with

Java debugging tutorial - 10 tips on debugging


in ...

http://javarevisited.blogspot.in/2011/07/java-debugging-tutorial-example-tips.html[8/23/2014 9:04:02 AM]

Java debugging tutorial - 10 tips on debugging in java with example


main method. In Eclipse just right click and select "Debug as Java Application".

10 practical Java debugging tips


Now let's see some java debugging tips which I used while doing debugging in Java in eclipse.

How to resolve
java.lang.UnsupportedClassVersionEr...

June
(
9
)

May
(
6
)

1) Use conditional breakpoint


Eclipse allows you to setup conditional break point for debugging java program, which is a breakpoint with
condition and your thread will only stop at specified line if condition matches instead of just stopping on
that line like in case of line breakpoint. To setup a conditional breakpoint just double click on any line where
you want to setup a breakpoint and then right click --> properties and then insert the condition. Now
program will only stop when that particular condition is true and program is running on debug mode.

April
(
10
)

March
(
4
)

February
(
10
)

January
(
10
)

2010
(
33
)

References
Java API documentation JDK 6
Spring framework doc
Struts
JDK 7 API
MySQL
Linux
Eclipse
jQuery
2) Use Exception breakpoint
How many times you have frustrated with a NullPointerException and you don't know the source from where
the exception is coming. Exception breakpoints are just made for such situation. Both Eclipse and Netbeans
allows you to setup Exception breakpoint. You can setup Exception breakpoint based on java exception like
NullPointerException or ArrayIndexOutOfBoundException. You can setup Exception breakpoint from
breakpoint window and your program will stop when you start it on debug mode and exception occurs.

3) Step over, Step Into


These are simply great debugging options available in any Java IDE, extremely useful if you are debugging
multi-threaded application and want to navigate step by step.
4) Stopping for a particular Thread
This is my own custom made java debugging tips which I made using conditional breakpoints. since most of
my projects are multi-threaded java programs and I want only a particular thread to stop on a particular
line, for doing that I setup a conditional breakpoint on that line and put
Thread.currentThread().getName().equals("TestingThread") and it works fantastically.
5) Inspect and Watch
These are two menu options which I use to see the value of expression during debugging java program. I
just select the statement, right click and inspect and it will show you the value of that statement at
debugging time. You can also put watch on that and that condition and its value will appear on watch
window.
6) Suspending and resuming thread
You can suspend and resume any thread while debugging java program from debug window. Just right click
on any thread and select either suspends or resume. This is also very useful while debugging multithreading program and simulating race conditions.

http://javarevisited.blogspot.in/2011/07/java-debugging-tutorial-example-tips.html[8/23/2014 9:04:02 AM]

Copyright by Javin Paul 2012. Powered by Blogger.

Java debugging tutorial - 10 tips on debugging in java with example


7) Using logical structure
Logical structure option is very useful for examining contents inside java collection classes like java hasmap
or Java Arraylist during java debugging. Logical view will show the contents like key and value of hashmap
instead of showing full details of hashmap which we may not be interested, you can enable and disable
logical view from variables window.
8) Step filtering
When we do Step Into on process debugging java program control goes form one class to other and it
eventually go to JDK classes like System or String. Some time we just to remain in our application and
don't want to navigate into JDK System classes in that case Step filtering is great you can just filter out JDK
class from Step into. You can setup step filtering from preferences JavaDebugStep Filtering and
enable and disable it from Debug window.
9) Copy Stack
While debugging java program if you want to copy the stack of a thread which hit the breakpoint and
suspended you do so by "Copy Stack" option. Just right click on Thread on Debug Window and select "Copy
Stack".
10) Last tip is use java debugging as last option and not the first option because its very time consuming,
especially remote java debugging which takes a lot of time if network latency is very high between local and
remote host. Try to identify problem by looking at code it would be very handy and quick.
lastly java debugging is real fun so definitely try it few times to get hold of it and please share some
other java debugging tips you use on your daily life.

Posted by
Javin Paul
at
6:35 AM

Labels:
core java
,
Eclipse

Location:
North America

15 comments
:

Anonymous
said...

Hi, I like your java debugging tutorial and in fact these are great tips on java debugging It would have been
better if you include images to show the debugging steps.

July 28, 2011 at 9:39 PM

Anonymous
said...

Debugging in Java is quite easy in Eclipse rather than Netbeans. I found debugging feature of Eclipse quite
useful than Netbeans. you can inspect, see logical structure and put watches quite easily in Eclipse. I was
initially searching for how to debug Java program in Eclipse and found this great article. keep it up.

October 17, 2011 at 12:50 AM

Anonymous
said...

Do you happen to have tutorials on how to debug a war file in eclipse? Thanks.

November 23, 2011 at 9:50 PM

Anonymous
said...

Having a section on how to debug Java program in Eclipse and separate on for debug Java program in Netbeans
will help your readers and also add value to the post.

December 6, 2011 at 8:59 PM

Ying Jin
said...

Thanks for sharing Java debugging techniques. I really appreciate as you don't learn debugging overnight and it
is one area which requires experience. by using your Java debugging techniques anyone can move forward. My
favorite debugging technique in Java as you mentioned is setting up conditional breakpoint. Also technique of
stopping any Thread is pretty useful.

February 29, 2012 at 10:14 PM

Javin @ sort array in descending order java


said...

@Anonymous, Thanks for your comment and glad to hear that you find this Java debugging tutorial useful.
Indeed attaching source of JDK, Spring and other common library make life much easy but if you want to avoid
the hassle than you can use JAD decompiler plugin in Eclipse, that will decompile class file for you.

March 7, 2012 at 4:14 AM

http://javarevisited.blogspot.in/2011/07/java-debugging-tutorial-example-tips.html[8/23/2014 9:04:02 AM]

Java debugging tutorial - 10 tips on debugging in java with example

Anonymous
said...

Hi,
Thanks for your valuable posts and can you post a discussion on Repositary server and how to work with the
repositary server in Real time.

April 11, 2012 at 9:42 PM

ketaru
said...

Hi Javin, are you aware of any new debugging feature or tips available for Java program in Eclipse Indigo and
Eclipse Juno ? I heard they introduced stopping on main as new feature on Eclipse Juno, Can you please show
an example of that.

August 29, 2012 at 9:36 PM

Anonymous
said...

One of the useful Java debugging Tips I would like to share is keeping watch on new
Exception().printStackTrace(). So when debugging thread stops on breakpoint it gives an idea from where do
you come from. I often used this Tip while debugging in Eclipse and Netbeans. This is very generic tips and you
can also use it on any IDE like IntelliJ IDEA.

January 22, 2013 at 1:13 AM

Anonymous
said...

Thanks a lot! Now I am in more love with Debugging.


But you should also mention "Change Value" option too. Which helps me a lot !

January 23, 2013 at 6:33 AM

Victor
said...

Just a note: the parameters -Xdebug and -Xrunjdwp are deprecated since Java 5, and have been replaced with
-agentlib:jdwp. See the docs for the new ones:
http://docs.oracle.com/javase/7/docs/technotes/guides/jpda/conninv.html#Invocation

January 29, 2013 at 4:55 AM

Hemanth
said...

Very good article. I'm debugging the code using Eclipse from long time, but never knew about some of these
wonderful tips. Thank you !!

March 1, 2013 at 7:34 AM

Rajesh
said...

In eclipse there is a hidden window called "Display", to enable it go to Window->show view->display.


Now, here is the interesting part, suppose that you wanted to know the value of variable "count" at that instant
but you forgot to place a println("the value is..."+count"), well you can have the value by using the Display
window.
Go to the Display window at the bottom of the IDE and inside of it type "count" (without quotes), then select
what you wrote and press ctrl+shift+D, you should have a response like this
output:
count
(int) 1
Value of variable "count" can be found even in other windows like "variable" or using other options like "inspect".
But another intersting advantage with this "Display" window is that, you can write piece of code (or complex
code) and find the output of it at runtime. for e.g.
I know few beginners are confused to decide whether to use ".equals" or "==", similarly there might be many
scenarios which we can dynamically run our code and decide which piece of code to use (or to find what is
going wrong).
e.g: (you can try a good example :-))
Integer temp = new Integer(1234);
if(temp.equals(new Integer(1234)))
System.out.println(" equals option works ");
else if (temp == new Integer(1234))
System.out.println(" == option works ");
else
System.out.println(" Both doesn't work ");
Output will be displayed on the console window. Hope this helps.

April 1, 2013 at 1:39 PM

http://javarevisited.blogspot.in/2011/07/java-debugging-tutorial-example-tips.html[8/23/2014 9:04:02 AM]

Java debugging tutorial - 10 tips on debugging in java with example

Ankur
said...

One another way which I do follow is, use loggers


It is useful when we cant remotely connect our live env and some exceptions occur, which cant be regenerate in
out test or local env. In such cases logs are very much useful to trace back all the steps and find out the root
cause.

November 10, 2013 at 9:01 AM

Saravana Kumar
said...

While debugging we can change the values of literals in values Window,it's one of the awesome features, I use
it often this.

February 15, 2014 at 4:48 AM

Post a Comment

Newer
Post

Home

Subscribe to:
Post Comments
(
Atom
)

About
Me

Privacy
Policy

http://javarevisited.blogspot.in/2011/07/java-debugging-tutorial-example-tips.html[8/23/2014 9:04:02 AM]

Older
Post

Vous aimerez peut-être aussi