Debug Java classes in Eclipse without their source file

Hello Readers,

This post will show how to debug Java classes in Eclipse when you don’t have their source. It can be used to see how various libraries/frameworks are written and give you better understanding about it. I had written a post on Jadclipse earlier that uses JAD, but JD-Eclipse is more advanced. JAD supports decompiling from command line which can be handy if you don’t have the project set up in Eclipse.

You’d need to download JD-Eclipse plugin for Eclipse (download link at the end) and copy it in Eclipse’s plugins directory. Then start Eclipse in clean mode to load the plugin.

Verify that decompiler plugin is installed. Go to Window –> Preferences –> Java –> Decompiler. You should see something like below:

Eclipse Decompiler

Then go into File Associations and make sure *.class files are associated to JD Class File Viewer.

File_Associations

I am going to use Apache’s commons-lang3-3.4.jar as an example. I have added the jar in classpath but not added its sources. So we will let JD-Eclipse decompile the classes and allow us to step through the source.

I wrote a basic HelloWorld class. It’s main method initializes 2 String objects and then uses StringUtils.equals() to check if they are equal. Using JD-Eclipse plugin, we can step through StringUtils.equals() method:

  1. HelloWorld.java. Put a breakpoint at the line that calls StringUtils.equals. When the execution stops at that line, you can step into (press F5) the source of StringUtils.equals method.

HelloWorld

2. You can step through this method and inspect variables/add expressions. Line numbers between /* */ indicates the class was compiled with debug info, which enables Eclipse to step through the code.

StringUtils_Source

3. Finally it calls equals() in java.lang.String. You cannot step into String.equals() as it was not compiled with debug info (javac -g). If you wanted to step through such code, you’d have to recompile the class with -g option. I have not tested this.

StringUtils_StepThrough

Below image shows how StringUtils and String classes look after they are decompiled. Notice String class does not have line numbers.

StringUtils_After_PluginString_After_Plugin

Downloads:

  1. JD-Eclipse plugin – Home page: http://jd.benow.ca/
  2. Apache Commons jar

Happy Debugging!

Advertisements

About Badal Chowdhary

I am a Software Engineer by profession. I have done SCJP and SCWCD certifications. Like working on cutting edge technologies and frameworks. Driven by challenges and fascinated by technology. I love playing and watching sports: Cricket, Ping Pong, Tennis, Badminton, Racket Ball and Gym.
This entry was posted in Eclipse and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s