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:
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.
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.
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.
Step 5: Now move the .class file and jad application to another folder.
Step 6: Type the command: jad DecompilerDemo
Above command generates a .jad file in the respective folder.
Step 7: Type the command: jad ?s java DecompilerDemo
It generates the .java file in the same folder.
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
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:
With javap command, many options are available. Some of them are:
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.
The following output shows what the class file has inside.
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.