Javatpoint Logo
Javatpoint Logo

Java Decompiler

A Java Decompiler is a special type of decompiler which takes a class file as input and produces Java source code as output. The decompilation is exactly the reverse process of compilation. Thus, decompiler does not produce a replica of the source code. It is because a bit of information lost from the source code during the compilation of the program.

But there are many complexities, for example, Java code is structured, and byte code is not structured. Moreover, the transformation is not one to one: two different Java programs may yield identical byte code.

There are following Java decompilers available:

  • DJ Java Decompiler
  • JReversePro
  • JODE
  • JD-CORE/ JD-GUI/ JD Eclipse

JAD Decompiler

The JAD is a command-line application. It is written in C++. The main function is to disassemble the .class files. JAD can be deployed from the console, and it will decompile Java classes into the source code. This way, developers can look deep into the code of Java runtime libraries and even beautify the lines, preparing them for a recompilation. There is a risk of overwriting of source code files, especially when the '-o' (overwrite output files without confirmation) parameter is used.

Example

Let's create a simple Java program which adds two number.

Follow the steps to convert the .class into readable Java source code. Here we are using JAD decompiler.

Step 1: Download JAD tool from https://bit.ly/2XuRzT5

Step 2: Extract the zip file and get jad.exe.

Java Decompiler

Step 3: Create a .java file (DecompilerDemo.java) in the same folder in which you have extracted the zip file.

Step 4: Compile and run the .java file. It will generate a .class file in the respective folder.

Java Decompiler2

Step 5: Now move the .class file and jad application to another folder.

Java Decompiler3

Step 6: Type the command: jad DecompilerDemo

Java Decompiler4

Above command generates a .jad file in the respective folder.

Java Decompiler5

Step 7: Type the command: jad ?s java DecompilerDemo

Java Decompiler6

It generates the .java file in the same folder.

Java Decompiler7

Open the DecompilerDemo.java we get the following code:

We can observe that written .java file is different from the generated .java file but gives the same output.

Java Class File Disassembler

javap Command

The javap is a Java command line tool which comes with JDK. It is used to disassemble the class file and prints a human-readable version of that class. Its output depends on the option used. Without using any option, javap prints the package, protected and public fields and methods of the classes passed to it. A javap is also used to find out which methods are available for a class if you don?t have the source code that was used to create the class. The general format of javap command is:

Options

With javap command, many options are available. Some of them are:

  • -c: It prints disassembled code.
  • -p: It shows all classes and members.
  • -s: It prints internal type signatures.
  • -constants: It shows static final constants.

Disassemble a class file using javap command

Generate the class file of the above program and use the following command to disassemble the class file.

Output

The following output shows what the class file has inside.

Java Decompiler8

But we do not get the Java source code by using the javap command. So there are many decompilers available which convert a .class file into a .java file.


Next Topic#




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA