Main Tutorials

How to decompile class in Java

This article shows how to decompile Java class in Eclipse IDE, IntelliJ IDEA, and command line.

Table of contents

1. Java Decompilers

Java decompiler can convert .class files back to its source code .java files or convert a program’s bytecode into source code. Below are some of the Java decompilers:

  1. FernFlower – IntelliJ IDEA build-in Java decompiler.
  2. JD Project – yet another fast Java decompiler, a GUI tool.
  3. Procyon – inspired by .NET ILSpy and Mono.Cecil.
  4. CFR – another java decompiler, written entirely in Java 6, decompile modern Java feature up to Java 14.
  5. Jad – no longer maintained, closed source.

2. Decompile Java class in Eclipse IDE

In Eclipse IDE, we can use Enhanced Class Decompiler plugin to decompile Java class files without source code directly.

2.1 Select Help -> Eclipse Marketplace... to search and install the Enhanced Class Decompiler plugin.

2.2 Select Window -> Preferences -> General -> Editors -> File Associations, configure the *.class without source default to Class Decompiler Viewer.

eclipse file associations

Now, click on the class or methods, press F3, and the plugin will automatically decompile the Java class.

Note
For more detailed in install and configure the Enhanced Class Decompiler plugin, visit this Java decompiler in Eclipse IDE.

3. Decompile Java class in IntelliJ IDEA

IntelliJ IDEA has a built-in Java decompiler using FernFlower. We no need to install or configure anything, just clicks on the Java class or method, clicks CTRL + B (declaration or usages) or CTRL + ALT + B (implementation), IntelliJ IDEA will automatically decompile the Java class.

java decompiler in idea

4. Decompile Java class in Command Line (FernFlower)

This example shows how to use FernFlower (Java Decompiler) to decompile a .jar or .class file in command line.

Note
The size of the official FernFlower is a bit large, difficult to build the source. The workaround is to use the fesh0r’s mirror build of FernFlower.

4.1 git clone the source code and Gradle build the source code into a fernflower.jar

Terminal

$ git clone https://github.com/fesh0r/fernflower

$ cd fernflower

# build the source code using Gradle build tool
$ gradle build

# the fernflower.jar at build/lib/

4.2 The below example uses fernflower.jar to decompile a asm-analysis-3.2.jar JAR file into folder /path/decompile/ (must exists).

Terminal

# decompile JAR or Class files
# java -jar fernflower.jar /path/asm-analysis-3.2.jar /path/unknown.class /path/decompile/

$ java -jar fernflower.jar /path/asm-analysis-3.2.jar /path/decompile/

INFO:  Decompiling class org/objectweb/asm/tree/analysis/Analyzer
INFO:  ... done
INFO:  Decompiling class org/objectweb/asm/tree/analysis/AnalyzerException
INFO:  ... done
INFO:  Decompiling class org/objectweb/asm/tree/analysis/BasicInterpreter
INFO:  ... done

The result is a new /path/decompile/asm-analysis-3.2.jar containing the source code *.java.

FernFlower decompiler

Extract the decompiled JAR file.

Terminal

$ jar -xf /path/decompile/asm-analysis-3.2.jar

5. References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments