Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

How to Decompile Java Classes

Decompiler, as the name suggests, is used to undo the effects of a compiler. Decompiler is used to get a .java file from a .class file. Decompiling helps us to understand the source code for a library. Some IDEs like IntelliJ IDEA come with built-in decompilers, while other IDEs like Eclipse can install and use other decompilers.

In this tutorial, we will learn how to decompile a class to get its source.

Decompiling Using Eclipse IDE

We can decompile class files in Eclipse by using the Enhanced Class Decompiler(ECD) plugin. ECD can be installed from the Eclipse Marketplace. Go to Help > Eclipse Marketplace, and search for Enhanced Class Decompiler.

Eclipse Marketplace

We also need to set up ECD to work properly. Go to Window > Preferences > General > Editors > File Associations, and select the "*.class without source" option. Select Class Decompiler Viewer as the default for it, and apply the changes.

ECD Setup

Next, we can decompile a class file by clicking on it. The following image shows the decompiled KeyEvent class. FernFlower decompiler is used for this purpose.

Decompiled Class

Decompiling Using IntelliJ IDEA

IntelliJ IDEA comes with a built-in decompiler that uses the FernFlower decompiler. We can open a .class file using IntelliJ, and it will automatically be decompiled to its source. Let's try to decompile the class file generated by the following source code.

public class Demo
{
	public static void main(String[] args)
	{
		int num1 = 5;
		int num2 = 15;
		double result = num2 / num1;
		System.out.print(result);
	}
}

The decompiled code is shown in the image below.

Decompiled Class

Decompiling Using the Command Line

We can also decompile JAR or class files from the command line. There are a few different tools that we can use for this.

JD-CLI

We can use JD-CLI to decompile our files using simple java command. Head over to the GitHub page of JD-CLI(https://github.com/intoolswetrust/jd-cli) to download and set up the decompiler.

java -jar jd-cli.jar [class-file]

FernFlower

We can also use the FernFlower decompiler to decompile JARs or class files using the command line. Clone the FernFlower repository(https://github.com/fesh0r/fernflower) and build the source code for it using the Gradle tool.

git clone https://github.com/fesh0r/fernflower
cd fernflower
gradle build

Next, we can run the following command to decompile a JAR file and store the contents in the mentioned directory.

java -jar fernflower.jar [path to JAR or .class file] [directory to store the decompiled files]

Javap

We can also use the javap command to view the disassembled code from a class file. It will not display any method implementations, but we can view the disassembled code for each method.

Let's consider that we compiled the following code into a Demo.class file.

public class Demo
{
	public static void main(String[] args)
	{
		int num1 = 5;
		int num2 = 15;
		double result = num2 / num1;
		System.out.print(result);
	}
}

The following image shows the output of a simple javap command.

Javap command

Let's view the disassembled code for the main method.

Disassembled code

Summary

Viewing the source code of compiled files is needed to understand the logic behind an application. We can decompile JARs or .class files by using IDEs like Eclipse or IntelliJ IDEA. Eclipse requires additional plug-ins to be installed, while IntelliJ IDEA comes with a built-in decompiler that uses FernFlower. We can also use popular command-line tools like JD-CLI or FernFlower to decompile files using IDEs.