Friday, April 15, 2011

Decompile Java Class file using decompilers.

Byte codes generated by javac compiler can again be converted into java source. For this we need a decompiler tool. Decompilers are the utilities that generate the source code from input java class file. A Decompiler knows about the structure of a Java class and parse it to generated Java source code.
Java decompilers will not give the exact Java source file from which class was generated and executed by Java Virtual Machine. But most of the structure will be maintained.

JAD: Java Decompiler

A lot of Decompilers are available in the market to decompile a class file. We will use one of the free decomipler called JAD.

I have used a simple Hello World java class to demonstrate JAD decompiler. Following is the code of HelloWorld.java file.
public class HelloWorld {
    public static void main(String args[]) {
        String fooBar = "Hello World from Java.";
        System.out.println(fooBar);
    }
}
Use following output when we use command line utility JAD to decompile our class file.
$> jad HelloWorld.class
Parsing HelloWorld.class... Generating HelloWorld.jad
The source file output of JAD is in file with .jad extension. You can change the extension to .java.
Following is the source that you get after decompillng our HelloWorld.class file.
// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3)
// Source File Name: HelloWorld.java
 
import java.io.PrintStream;
 
public class HelloWorld
{
 
public HelloWorld()
{
}
 
public static void main(String args[])
{
String s = "Hello World from Java.";
System.out.println(s);
}
}

Note that the string variable that we used in our original Java class was fooBar and it got changed into s.

No comments:

Post a Comment

Chitika