Java Decompiler
Some curious minds wonder whether there is a way to decompile a java bytecode to source code. Yes, the way to decompile a java byte code is by using a java decompiler. First, let us start with knowing what decompilation is. Decompilation is nothing but converting a java bytecode or class files to human-readable java source code. In simple terms, decompilation is the reverse operation of compilation.
In today’s tech world, there are many java decompilers available online. Now, let us dive deep into the article to learn more about java decompilers and the steps to use some popularly used decompilers in java.
What is Java Decompiler?
Java Decompiler is a specific decompiler tool used to reverse engineer a java program. In simple terms, the java decompiler takes the bytecode or class files as input and produces java source code as output. Since the java bytecode contains a high-level representation and symbolic information than the conventional object code, it is easier to decompile it back to the java source code.
While compiling a java source code, we will lose some pieces of information, so while decompiling it, we will not get the exact copy of our source code. Additionally, the java byte code is not structured.
When to use a Java Decompiler?
Imagine you are stuck in a situation where you need to understand the code behaviour. You are left only with class files. Your source code is also unavailable. Here the Java Decompilers come to your rescue. It is necessary to get proper permission to decompile others’ source code.
When you accidentally lose your source codes and your backup is not working, if you have access to the deployed class files, you can get back the java source code by using Java decompilers.
Importantly, you will be facing a situation where you need to analyse the third-party jar file’s source code to debug the issues. There you have to use the java decompilers. In addition, you can also use the java decompilers to view the source code of java inbuilt API classes to enhance your programming knowledge.
Commonly used Java Decompilers
Some of the popularly used java decompilers are:
1. JDProject
JD Project supports Java 5 and later versions. Written in C++, so it is efficient and faster. It operates as a standalone utility as it is independent of JRE (Java Runtime Environment). It is a free tool for non-commercial use.It includes JD-Core, JD-GUI, JD-Eclipse and JD-IntelliJ. We can integrate this decompiler with Eclipse and IntelliJ IDEA IDEs.
2. Procyon:
Procyon is an open-source java decompiler developed by Mike Strobel. The speciality of this decompiler is it deals with java and all other latest versions. In addition, it handles the new features of Java8 like Lambda expressions, method references, enum, annotations, and others.
3. Cavaj:
Cavaj is a Java decompiler specifically designed for Windows. So it will not support other operating systems like Linus, Mac, etc. It is a free decompiler with a simple GUI. This Java decompiler gives accurate source code outputs.
4. CFR:
CFR is an open-source java decompiler written in java6. It contains a command line tool that makes the decompiling process efficient and faster. Moreover, it handles various new modern features of Java like Lambdas, dynamic constants, modules, switch Expressions, etc. So it is an ideal option for all the latest java version users, that includes Java 7, 8, 9, 10, etc.
5. Androchef:
Androchef is a Java decompiler specifically designed for Windows with a user-friendly GUI. It handles complex bytecodes, applets, APK, JAR, and DEX files.
6. FernFlower:
FernFlower is the analytical built-in java decompiler for IntelliJ IDEA developed by Egor Ushakov.
7. Neshkov-DJ java compiler:
Neshkov is a Windows-based free decompiler with a simple GUI. The main advantage is it can operate without JDK installed in the system.
Decompiling in Eclipse IDE:
We use the Enhanced Class Decompiler(ECD) plugin in Eclipse IDE for decompilation. This plugin includes JD, FernFlower, CFR, JAD, and Procyon decompilers. Following are the steps to decompile the class files in eclipse IDE.
1. Search and Install the Enhanced Class Decompiler(ECD) by selecting Help → Eclipse Marketplace.
2. After installing the plugin, restart the eclipse IDE.
3. Go to Windows→ preferences → general →editors → File Associations.
4. Select *.class without source in the file types area.
5. Select “class decompiler viewer” in the Associated editor’s area.
6. Click the Default button.
7. Click the Apply and Close buttons.
8. Finally, open the corresponding class file.
Now the corresponding class file is automatically decompiled to Java Source code.
Decompiling in IntelliJ IDEA IDE:
The IntelliJ IDEA IDE has a FernFlower decompiler as a built-in decompiler. So, no configuration is required to use the decompiler. There are two methods available. They are as follows.
Method 1:
Click on the corresponding Java class and Click CTRL+B to view the declarations. Then, click CTRL+ ALT+ B to view the implementations.
Method 2:
CTRL + Left-click on the corresponding class name to view the source code.
Decompiling using JD-GUI:
JD-GUI is a graphical utility that uses JD-CORE as its decompiler library.
The steps are as follows:
1. Download the JD-GUI jar file.
2. Open the command prompt.
3. Navigate to the folder containing the jar file.
4. Use the following command to run the jar file.
java -jar jar_fileName.jar
Click enter.
5. This results in opening the User Interface of JD_GUI.
6. Select “File→ Open File” and open the corresponding class file to be converted.
Now the source code is generated for the corresponding class files using JD_GUI.
Decompiling in Command Line Interface:
We can use the command prompt to decompile the java class files to source codes.
JD-CLI:
Initially, download JD-CLI decompiler.
Then use the following command in the command prompt to decompile the class files.
java -jar jd-cli-jarname.jar file_path/file-name.class
Now the source code is generated for the corresponding class files using JD-CLI.
Javap Command:
In addition, we can use the javap command to view the disassembled code.
javap file_path/ClassName
For Example:
Consider the following code saved in a file named FirstCode.java.
import java.util.Scanner; class FirstCode { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Welcome to FirstCode"); System.out.println("Enter the first Number"); int number1= scan.nextInt(); System.out.println("Enter the second Number"); int number2= scan.nextInt(); int sum=number1+number2; System.out.println("The sum is" + sum); } }
Now open the command prompt. Navigate to the folder that contains the JDK bin.
For Example:
cd C:\Program Files\Java\jdk-17.0.1\bin
Then type javap E:\FirstCode. Click Enter. Now you can see the disassembled code in the command prompt.
Summary
There are many online java decompilers available. It is recommended not to use it for production-level codes because that may cause data breaches and security issues. If you are a learner, you can use online decompilers for decompiling some beginner-level learner codes. I hope you gained some fundamental knowledge in Java Decompilers.