I started learning Java. But, until recently, I had no idea about the same. I didn't knew that something magical existed that would perform thisconversion. But now since I know a bit, I thought I better share it.

Java Decompiler

As you would have guessed by now, a Java Decompiler is a computer program capable of reversing the work done by a Compiler. In essence, it can convert back the Bytecode (the .class file) into the source code (the .java file).

There are many decompilers that exist today, but we will talk about the most widely used JD - Java Decompiler, which is available both as astand-alone GUI program and as an Eclipse-plugin.

To install and use these tools are a breeze and would not take you more than a few minutes to get accustomed to it. Hence, I would not repeat the process that's already mentioned on their site.One thing that we must note here is that the process of conversion mightNOT result into 100% exact code, i.e. the generated Java file might not match the actual Java code character by character. However, most of the code would be replicated but things like variable & function names or some other minor details may differ.L

ets have a look at the JD-GUI, stand-alone tool, written in C++;making it pretty fast to execute(decompile) and display the result. Also, it isindependent of the the Java Runtime Environment and thus no setup is required to install it.

Lets test the tool now.

Example Java Code:

public class test{
public static void main(String[] args){
System.out.println("Hello world");
}
}

Compile it: javac test.java

so that we have ByteCode (test.class file) with us now.

Decompile it using any one of the following ways:

Execute the following on the command line: jdi-gui.exe test.class

Select 'Open File' from the menu, browse to get to the test.class file and open it

Drag and Drop the test.class file into the JD-GUI tool

All of the above situations result in generating the following Java Code, have a look:

import java.io.PrintStream;
public class test{
public static void main(String[] paramArrayOfString){
System.out.println("Hello world");
}

Well just by seeing JD perform really well on this simple example,we cannot decide how efficient is this tool. But, still it is a tool thatevery Java Developer must be aware of. Because, in case you have accidently deletedyour Java files and are left with only the .class files, this is the tool that can save your neck.


Like it on Facebook, Tweet it or share this article on other bookmarking websites.

No comments