O'Reilly logo

Java Cookbook by Ian F. Darwin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Decompiling Java Class Files

Problem

You lost the source code.

Solution

If you still have the class files, decompile them.

Discussion

Have you ever looked at a class file by accident? Open it in a text editor, for example, and you might see this. You’ve never done this by accident, right? Sure, I believe you . . .

^H^@Z^C^@^@^@P^H^@[^H^@n^H^@o^H^@p^H^@q^H^@r^H^@s^H^@t^H^@v^H^@y^H
^@z^H^@{^H^@}^H^@Ç^H^@ä^H^@à^H^@á^H^@ª^H^@º^G^@ç^G^@Æ^G^@ô^G^@ö^G^@ò^G^@Û^G^@ù^G
^@ÿ^G^@...^G^@Ü^G^@¢^G^@£^G^@¥
^@^V^@@
^@^\^@@
^@!^@A
^@^Y^@B
^@^[^@C

There’s no resemblance to the Java source file that you wrote and spent so long fussing over the formatting of. What did it get you? Nothing here. The class file is a binary file that can’t be inspected easily. However, it is in a well-documented format, and there’s the rub. Once a format is known, files can be examined. One example of a Java program that examines other Java programs is javap, which gives you the external view of a class file. I’ll show you in Section 25.3 just how this part of javap works and how you can write your own tools that process other Java classes. Meanwhile, this discussion is about decompilation. Let’s suppose you have put some meat through a meat grinder. It’s been converted to zillions of little bits. It might, in fact, look a bit like the class file seen here. Now suppose that unbeknownst to you, your paycheck fell into the meat and went through the grinder. Ugh! But the real question is, can you put the paycheck back together from the little pieces in the output? A related question is whether you can put a Java source file back together from the little pieces in the class file.

The task seems impossible. The file appears inscrutable. How can it be un-ground? But computer geeks like to work with files, and restoring structure to them is one part of that. When the infamous Internet Worm struck in 1988, it was only a matter of hours before security experts had taken the binary compiled program -- most OSes’ equivalent of a class file -- and turned it back into source code without any tools other than debuggers, dumps, and manuals. So it is possible to take an object file and turn it back into some kind of source file. Now the ground-up paycheck, if you find the pieces and tape it back together, will still have bumps (not to mention the smell of salami or pastrami as appropriate). And a decompiled file will have one major bump: no comments! All the comments will be gone. But hopefully you can get back something that will take the place of your lost source file.

The first tool for reverse compilation of Java class files was called Mocha . Written by the late HanPeter van Vliet of the Netherlands, this tool showed a generation of early Java hackers that it was possible to decompile Java. Here is HelloWorld and its decompilation:

/**
 * Your basic, minimal, Hello World type program in Java.
 */

public class HelloWorld {
    public static void main(String[] argv) {
        System.out.println("Hello, World");

    }
}

The result of compiling it and then decompiling it is:

/* Decompiled by Mocha from HelloWorld.class */
/* Originally compiled from HelloWorld.java */

import java.io.PrintStream;

public class HelloWorld
{
    public static void main(String astring[])
    {
        System.out.println("Hello, World");
    }

    public HelloWorld(  )
    {
    }
}

Perhaps not as pretty, and with less of the abbreviation that is common practice in Java. The null constructor for HelloWorld actually does exist in the compiled class (as you can verify by running javap on it), so Mocha dutifully generates it.

Well, Mocha is OK, and the price is right -- it’s free. However, I did mention that it’s no longer being maintained; it reportedly has problems with some of the class file constructs generated by current compilers. The O’Reilly web site for this book includes a link to Mocha.

A newer tool is Jad , written in C++. Jad is free but closed source (available in binary only); see http://www.geocities.com/SiliconValley/Bridge/8617/jad.html. There are also several commercial decompilers that keep abreast of the latest versions of Java; check one of the Java resource sites or magazines for the ones that are currently available.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required