How do I decompile Java classes in the maven central repository?
Author: Deron Eriksson
Description: This tutorial describes how to decompile Java classes in the maven central repository using Jarvana.com and JD-GUI.
Tutorial created using: Windows XP


Decompiling JavaSW classes is one of the most powerful, useful tools that developers can use when debugging certain difficult problems in Java applications. The mavenSW central repository contains millions of Java classes. Often, these classes have accompanying source code and javadocs, but more often than not, source code isn't available for these classes. Wouldn't it be nice if you could quickly and easily find any Java class in the maven repository and decompile that class? Now you can, with a couple free tools.

The technique can be broken down into two steps: (1) find a Java class, and (2) decompile the Java class.

Step 1: Find a Java class

Classes in the maven central repository can be found using the class-searching capabilities of http://www.jarvana.com, a web-based maven repository search tool that I wrote. As an example of its use, below I'll perform a search for the class "org.apache.commons.lang.StringUtils".

Jarvana maven repository search engine

The search results for the "org.apache.commons.lang.StringUtils" class in the maven repository are shown below. There were 113 class files found that matched the search criteria. Notice that several of the search results also have corresponding source code and javadocs available. These can be viewed by clicking on the "J" and "@" icons, respectively.

Notice that the source code and javadocs are not available for all of the class search results. However, the class file itself can be accessed by clicking on the icon with 0's and 1's on it. Below, I have moused-over the StringUtils class file in the commons-lang version 1.0.1 jarW file. So, how do we decompile this class file?

Jarvana search results for 'org.apache.commons.lang.StringUtils'

Step 2: Decompile the Java class

To decompile Java classes, we need a Java decompiler. JD-GUI is a cool Java decompiler standalone application available for download at http://java.decompiler.free.fr/. It is available for Windows, Linux, and Mac. I downloaded the Windows version from http://java.decompiler.free.fr/jd-gui/downloads/jd-gui-0.2.10.windows.zip. You can unzip it anywhere. I unzipped it to my desktop.

JD-GUI on my desktop

Next, we need to associate .class files with JD-GUI. To do so, I go to Folder Options in the Windows Explorer.

Going to Folder Options in Windows Explorer

If a CLASS extension isn't present in the list of extensions on the File Types tab, create the new CLASS extension.

Creating new CLASS extension

Now, we need to set the association for CLASS files. To do so, I click the Change button.

Setting the CLASS file associations via the Change button

I browsed the file system (via the Browse... button) and located and selected jd-gui.exe. It now appears in the list of Programs, so I select it and click OK.

Selected jd-gui from the list of programs

Java class files are now associated with the JD-GUI decompiler. I click the Close button.

.class files are now associated with JD-GUI

Now, we can decompile our Jarvana class search results! As an example, in Firefox I'll click the icon for the org.apache.commons.lang.StringUtils class in the commons-lang-1.0.1.jar file.

Clicking on icon for the org.apache.commons.lang.StringUtils class

This brings up a Firefox 'Opening StringUtils.class' dialog box. I click the 'Open with' radio button and then click the OK button.

Firefox 'Opening StringUtils.class' dialog box

This opens up the decompiled Java class in JD-GUI.

Viewing org.apache.commons.lang.StringUtils from commons-lang-1.0.1.jar in JD-GUI

That's all there is to it! Find the Java class file you're interested in via Jarvana.com and then click on the class file icon in your browser. This will open the decompiled class in JD-GUI automatically since we've associated class files with the JD-GUI decompiler application.

Of course, before decompiling a class, you should read its licensing information to be sure that it is fine to decompile the class.

NOTE: One thing to be careful about is decompiling classes with the same name. If you decompile multiple classes and the browser saves those files to the same directory, they may be named similar to the following example (StringUtils.class, StringUtils-1.class, and StringUtils-2.class):

Downloading multiple StringUtils.class files

In this case, even though StringUtils-1.class and StringUtils-2.class were downloaded later, the decompiler will still display the decompiled results for the very first StringUtils.class, even when you would expect to see the results for StringUtils-1.class and StringUtils-2.class. This also happens if you click on the StringUtils-1.class and StringUtils-2.class files, which end up opening StringUtils.class in the decompiler. This can be very confusing at first.