Thursday, August 26, 2010

Java Decompiler : Simple Java Program to decompile .class files

I was wondering is there a java program to decompile a .class file of another java program or itself. I searched on internet but I didn't found any, latter I decided to write my own. but how..... lets see.

When we compile a java file, JVM creates .class file. But imagine you have a .class and you need to create its source .java file, in such case you can use java decompiler which creates .java file of a .class file.


However many times decompiled .java file is not 100% accurate but still more or less you will get idea about what the java file consist of.

There are many java decompiler available on internet.We will use JAD Java Decompiler to decompile our .class files because it can be easily integrated with our Java program.

To know more about JAD Decompiler click here, Also you can download latest version of JAD Decompiler. In our example we are using JAD 1.5.8 version ie jad158g.win.zip. Visit  http://www.varaneckas.com/jad to know more.

Installation

Unzip jad.zip file into any appropriate directory on your hard drive.
This will create two files:

    * an executable file named 'jad.exe' (Windows 9x/NT/2000) or 'jad' (UNIX)
    * README file 'Readme.txt', which contains the short user's manual

Note : You need to add jad.exe in your CLASS PATH
Simple way just put jad.exe in your java path For Eg : C:\Program Files\Java\jdk1.6.0_21\bin\jad.exe

To test your installation just type 'jad' in your command prompt, it should produce below output.




Now Lets see the decompile process and its command.

For example, lets say we have Test.class to which we need to decompile.

C:\Jad>jad Test.class
Parsing Test.class... Generating Test.jad

This will create Test.jad file which contains java code, don't worry .jad is default extension.

C:\Jad>jad -s .java Test.class
Parsing Test.class... Generating Test.java

using -s you can provide extension of the file. This will create Test.java.

Also you can provide destination folder where you want to decomiple the .class file.

C:\Jad>jad -s .java -d C:\Dest Test.class
Parsing Test.class... Generating C:\Dest\Test.java

This will create Test.java in our desire destination folder. To know more see the 'Readme.txt' file.

 Now we can write our java program which will use above mentioned system commands and can be used as Java decompiler program.

/* JadExample.java */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class JadExample {

    public void runSystemCommand(String command) {
       
        try {
            Process p = Runtime.getRuntime().exec(command);
           
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
           
            BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
           
            String s = "";
           
            System.out.println("command :: "+command);
            while ((s = stdInput.readLine()) != null) {
                System.out.println(s);
            }
           
            while ((s = stdError.readLine()) != null) {
                System.out.println(s);
            }           
           
        } catch (IOException e) {
           
            e.printStackTrace();
        }       
    }
   
    public void decompile(String srcClassFile,String destFolder) {
       
        runSystemCommand("jad -s .java -d "+destFolder+" "+srcClassFile);
       
    }
   
    public static void main(String[] args) {
       
        JadExample obj = new JadExample();
       
        //obj.decompile("Test.class", "."); //Current directory
        obj.decompile("Test.class", "C:\\Test");
    }

}





Run the above program and see the output. If you like it do write comments or mail me madan712@gmail.com