Java - How to decompile a ".class" file into a Java file (jd-cli decompiler)

Jar files are zipped. After unpacking you will find many .class files. class file is an output generated by compiling Java files into bytecodes.

The Java code is not visible when you open the class file, so if you want to see it, you have to decompile it back to Java.

There are several ways to decompile, but I will introduce the following two methods here.

  1. How to decompile in IntelliJ IDE
  2. How to decompile using jd-cli tool

1. How to decompile in IntelliJ IDE

One of the easiest ways to decompile a .class file into a Java file is through IntelliJ.

If you are using IntelliJ, simply drag-and-drop the .class file onto the IDE window and it will automatically decompile to Java. Decompile class to java in Intellij

2. How to decompile using jd-cli tool

There is a tool called jd-cli that decompiles class files into Java. It is convenient because it converts all class files in the jar file to Java.

The jd-cli tool can be downloaded from GitHub - Jd-cmd.

I unzipped the installation file to ~/apps/jd-cli-0.9.2-dist/ and saved it.

Then you can run the jd-cli.jar command as follows. Just input the jar file you want to decompile and the location where the converted file is saved as an argument to the command.

$ java -jar jd-cli.jar [jar file] -od [output folder]

In my case, I can decompile it with the following command: If you look in the output folder, you can see that it has been decompiled into a java file.

java -jar ~/apps/jd-cli-0.9.2-dist/jd-cli.jar android.jar -od output
$ jd-cli android.jar -od output
09:13:41.923 INFO  jd.cli.Main - Decompiling android.jar
09:13:41.925 INFO  jd.core.output.DirOutput - Directory output will be initialized for path output
09:13:46.225 INFO  jd.core.output.DirOutput - Finished with 4751 class file(s) and 7594 resource file(s) written.

Decompile only one class file

jd-cli has commands to convert class files to Java as well as jars.

  • java -jar jd-cli.jar [class file] : Converts class file to java and displays it on the screen
  • java -jar jd-cli.jar [class file] -od [output folder] : Converts class file to java and saves it in ouput folder

You can use it like this:

$ java -jar jd-cli.jar AbstractCollection.class
09:32:37.885 INFO  jd.cli.Main - Decompiling /home/js/Desktop/android/java/util/AbstractCollection.class
package java.util;

import androidx.annotation.RecentlyNonNull;
import androidx.annotation.RecentlyNullable;

public abstract class AbstractCollection<E>
....

alias (For linux user)

Since it is cumbersome to enter a command every time, you can register the command as an alias in ~/.bashrc as shown below.

$ alias jd-cli='java -jar ~/apps/jd-cli-0.9.2-dist/jd-cli.jar'

If you register an alias, you can decompile it with a short command as follows.

$ jd-cli android.jar -od output
codechachaCopyright ©2019 codechacha