How to Decompile Java Class

Haider Ali Feb 02, 2024
  1. Reasons for Decompiling a Java Class
  2. Java Decompiler
  3. Decompile Java Class Using Eclipse IDE
  4. Decompile Java Class Using IntelliJ IDEA
  5. Decompile Java Class Using Command Line
  6. Decompile Java Class Using JD-GUI
How to Decompile Java Class

In this guide, we will learn about the art of decompiling a Java .class file. We will see a brief overview of decompiling, how we decompile a .class file in Java, and what changes we may see when we decompile a file in Java.

Reasons for Decompiling a Java Class

At least once in their career as Java developers, they come across a situation where they have to face JAR files or where they have to decompile a .class file back to a source code.

As we know, JAR files is a Java Archive Package with a collection of different Java class files and other resources such as images, audio, etc. These files are built in a ZIP format and use the extension .jar.

The problem with JAR files is that a user is not granted permission to open this file. The only way to access these files is if the user decompiles the JAR file.

To be able to read the .class files in the file, we will have to use software tools to decompile this file.

On the other hand, with .class files, we can use software like Eclipse IDE and IntelliJ IDEA. Also, command line arguments can be used to decompile a .class file.

Java Decompiler

Decompiling comes under the action of reverse engineering as we are decompiling a JAR file into a readable class file. We will learn about this later in this guide but first, let us look into the .class files.

There are multiple ways to decompile a .class file. It comes down to what’s the most suitable for the user working with these files.

There are software tools that help with this process, but there are also other ways we can decompile a .class file into our source code. Two of the methods we are using are software like Eclipse IDE and IntelliJ IDEA.

The most important advantage of using these two is that the decompilers are very easy to use. Let us look into the most commonly used software, Eclipse IDE.

There are some steps that you will need to follow for this.

Decompile Java Class Using Eclipse IDE

A plugin is used to decompile a .class file; in this software, the plugin is called Enhanced Class Decompiler (ECD). Enhanced Class Decompiler can be installed easily from the software’s marketplace, called Eclipse Marketplace.

  • To do that, we will go to the Go to help option and select Eclipse Marketplace.
  • After this, search for Enhanced Class Decompiler. After this, we need to set up the ECD to work properly.
  • To set up the ECD, we click on the Go to Window option from the menu and then click on Preferences.
  • Then select General, where you will find the option Editors from the dropdown.
  • After clicking on the previous option, select File Association in another dropdown.
  • Then finally, select the *.class without source option.

A .class file can now be decompiled by just clicking on it.

Decompile Java Class Using IntelliJ IDEA

The main reason to use this software is that we don’t need to download or set up a decompiler. This software has a default decompiler, making it easier for the user to use IntelliJ IDEA.

All we have to do is open a .class file in IntelliJ IDEA, which will automatically decompile that file.

Decompile Java Class Using Command Line

Another method to decompile a .class file is using the command line. This method comes with three options. It is totally up to the user to choose from.

Use JD-CLI

JD-CLI is used for single command line statements. Only a single command is needed to decompile a class file.

This is the command used to decompile a class file:

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

We can download this decompiler by clicking here: intoolswetrust/jd-cli: Command Line Java Decompiler (github.com).

Use FernFlower

FernFlower is another decompiler downloaded to the system to decompile a .class file.

We would have to clone this decompiler to be able to use FernFlower. We do that by clicking here: fesh0r/fernflower: Unofficial Mirror of FernFlower Java Decompiler (github.com).

These are the commands used to clone:

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

To decompile a file, use this command.

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

Decompile Java Class Using JD-GUI

Decompiling a JAR file is different from a .class file. Java decompiler is the most commonly used software tool to decompile a JAR file.

This tool can easily be downloaded. Different versions of this software can be downloaded as the user requires by clicking here.

Among these versions, JD-GUI is the best-known UI version of the Java decompiler. JD-GUI is an application that will allow to user to see the hidden class files in JAR files.

Note that low-level information, such as comments in a .class file, will disappear. There are also chances of some original variable names disappearing, but that depends on what option the user chose while compiling.

If a debugger was used while compiling, rest assured that the .class file will be in a better readable form as we do not get the same source code.

Let us understand this concept better with the help of a simple Hello World example using comments.

class Example1 {
  public static void main(String[] args) {
    // This is the comment used.
    System.out.println("Hello World");
  }
}

When we decompile this HelloWorld file that is right now in the form of a JAR file, we will get this .class file as an output.

Output:

class Example1
{
    public static void main(String[] paramArrayOfString){System.out.println("Hello World");}
}

As you can see, the clear difference between these two files is that the comments have disappeared and the visible change in the parameter of the main method.

In the end, it depends on the user on what software tool to use to decompile a Java JAR file, as there are plenty of options.

Author: Haider Ali
Haider Ali avatar Haider Ali avatar

Haider specializes in technical writing. He has a solid background in computer science that allows him to create engaging, original, and compelling technical tutorials. In his free time, he enjoys adding new skills to his repertoire and watching Netflix.

LinkedIn

Related Article - Java Class