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
- 2. Decompile Java class in Eclipse IDE
- 3. Decompile Java class in IntelliJ IDEA
- 4. Decompile Java class in Command Line (FernFlower)
- 5. References
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:
- FernFlower – IntelliJ IDEA build-in Java decompiler.
- JD Project – yet another fast Java decompiler, a GUI tool.
- Procyon – inspired by .NET ILSpy and Mono.Cecil.
- CFR – another java decompiler, written entirely in Java 6, decompile modern Java feature up to Java 14.
- 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
.
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.
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
$ 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).
# 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
.
Extract the decompiled JAR file.
$ jar -xf /path/decompile/asm-analysis-3.2.jar