Decompile Java Class file using decompilers.

      

duke-java-mascotByte 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.

You can download JAD compiler from here. Update: JAD’s site is down hence you can download it from this link.

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.

Any idea why so?


Facebook  Twitter      Stumbleupon  Delicious
  

10 Comments on “Decompile Java Class file using decompilers.”

  • q666 wrote on 20 January, 2009, 17:33

    The reason for the change is Refactorization/Optimization , when the compiler is compiling the source it`s grabbing the long variable names in this case “FooBar” and replace it to some shorter versions for example “s” i think that most of the compilers are doing it.

  • jakob wrote on 3 February, 2009, 19:24

    Your wordpress theme generates an error:

    Warning: ob_start() [ref.outcontrol]: output handler ‘ob_gzhandler’ cannot be used twice in /home/viralpat/public_html/blogs/wp-content/themes/wp-max/header.php on line 1

  • Viral Patel wrote on 3 February, 2009, 19:53

    Hi Jakob,
    Thanks for pointing out the error. Actually I was trying to enable GZip compression for my website and hence was playing with .htaccess ;)
    Are you still getting the error?

  • prashant wrote on 3 March, 2009, 17:42

    Hi Viral,

    It is very good tool.
    But currently it is not existing.

    Here i have a lot of list of available decompilers.
    Just see,It is very useful

    http://yuvadeveloper.blogspot.com/2009/03/java-decompilers-class-to-java-file.html

  • kungfugeek wrote on 5 March, 2009, 0:41

    The page http://www.kpdus.com/jad.html#download expired in late February. Is there another place I can dl the executable?

  • Viral Patel wrote on 6 March, 2009, 19:26

    Thanks kungfugeek, I have updated the link. You can download JAD from this site.

  • freddieMaize wrote on 20 March, 2009, 12:25

    hope you will not mind if I link your “jad.zip” download from my Blog

    Thanks
    Freddie

  • Viral Patel wrote on 20 March, 2009, 13:05

    Hi FreddieMaize, You can link to jad.zip from your site. No problem :)

  • ivmai wrote on 15 December, 2009, 17:30

    To achieve better results (including for Java 1.5+ classes) in decompilation, process the classes with JadRetro (jadretro.sf.net) tool before decompiling them by Jad.
    Of course, it doesn’t help in your case with “fooBar” variable because javac doesn’t put local variable names to the generated class file unless instructed to do so (-g:vars).

  • sharmabhabho wrote on 19 February, 2010, 15:53

    Thank you for sharing so much information. May be this information would help me in developing an application in future. Right now i am excited to go to attend the Sun Tech Days 2010 conference in Hyderabad. Experts are going to share ideas on new technologies there.

Write a Comment

Gravatars are small images that can show your personality. You can get your gravatar for free today!

Copyright © 2010 ViralPatel.net. All rights reserved.