I've been amazed recently at just how helpful and useful a java decompiler can be. If you don't know what a decompiler is, or you do but haven't taken the time to download one, let me start by saying you're missing out! A decompiler is a tool that lives up to its name - it does the opposite of a compiler by taking compiled .class files and converting them into source .java files. This is accomplished by using the Java classfile format as a key for decrypting the file into java "plain text". Now, here are just a few of the reasons why you would want to do this (all of these reasons assume you don't have the source):
- You have a jar file or collection of .class files that you must utilize; however, the code has little to no documentation.
- The code you are re-using isn't behaving as expected and you believe the problem is a bug; however, you can't diagnose the root cause or avoid the problem altogether by modifying how you interact with the code.
- You aren't sure what version of your own code is deployed in an integration or test environment. You recently made some changes to this code; however, you're not sure what version is actually deployed.
- You need to understand the architecture and algorithms of the re-usable code in order to make significant design decisions.
I could go on and on - but if you want more reasons, please go to The Decompilation Wiki and click on: Why Decompilation. This site is an excellent knowledge base on decompilers - it contains decompilation techniques, history, legal concerns, etc.; it also maintains a full listing of java decompilers as well as decompilers for other languages. Instead of diving into comparing and contrasting each decompiler, I'll just make a long story short: cavaj is the best one available.
I've downloaded almost all of the ones in the above listing, but none work as well as cavaj; its best feature is the windows install that allows you to simply double-click on a class file in order to decompile it. But, by all means, if you have a decompiler that you like better, please share by adding a comment! Each decompiler is unique simply due to the fact that decompiling code is a bit like trying to unscramble an egg. As a result, decompilers do not reproduce the same source code given identical class files. As an example please see how Jode and cavaj decompile a toString() method differently:
cavaj:
public String toString()
{
StringBuffer buf = (new StringBuffer(command)).append(' ');
CommandOption option;
for(Iterator iter = options.iterator();
iter.hasNext(); buf.append(option.toString()).append(' '))
{
option = (CommandOption)iter.next();
}
buf.setLength(buf.length() - 1);
return buf.toString();
}
Jode:
public String toString() {
StringBuffer buf = new StringBuffer(command).append(' ');
Iterator iter = options.iterator();
while (iter.hasNext()) {
CommandOption option = (CommandOption) iter.next();
buf.append(option.toString()).append(' ');
}
buf.setLength(buf.length() - 1);
return buf.toString();
}
One further note - a disclaimer really - before you decompile someone else's code, please make sure you understand the legal ramifications of doing so. Even though open source code is so prevalent, there are still many proprietary libraries that explicitly specify decompiling or reverse engineering their code is not allowed and is illegal.
Comments
I second Mike's comment on JadClipse. Being able to seemlessly trace into any Java code, regardless of whether you actually have the source available is awesome. If you're an IntelliJ user, you can do the same thing with IdeaJad.
Cavaj is actually a front-end to the jad decomiler. There are plenty of others. I'm partial to DJ Java Decompiler myself.
Eclipse users should also take a look at JadClipse for integration into your IDE.
Post new comment