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:
Then go into File Associations and make sure *.class files are associated to JD Class File Viewer.
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:
- 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.
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.
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.
Below image shows how StringUtils and String classes look after they are decompiled. Notice String class does not have line numbers.