Have you ever found yourself in one of these situations?
- "I need to check the logic inside a library, but I don't have the source code."
- "I accidentally lost my
.javafiles and need to recover them from compiled.classfiles."
In the Java ecosystem, this is where Decompilation comes to the rescue. While developers used to rely on standalone tools like JD-GUI, modern IDEs now have powerful decompilers built right in, and specialized tools have evolved to handle the latest Java features.
In this article, we’ll explore how Java decompilation works, how to use it within VS Code, IntelliJ, and Eclipse, and which tools are best for modern development.
What is Java Decompilation?
Decompilation is the process of converting a compiled binary (bytecode) back into human-readable source code.
In Java, this means analyzing the .class files generated by the compiler and reconstructing the original .java source code.
Why do we need it?
- Auditing Libraries: Understanding the internal logic of a JAR file when documentation is lacking.
- Source Code Recovery: Recovering logic from a compiled build after accidental data loss.
- Debugging & Verification: Ensuring the
.classfile running in production matches the source code you have on hand.
Note: Decompiled code is rarely a 100% match to the original. Comments and local variable names are often stripped during compilation. It is a tool for understanding logic, not for cloning software.
Method 1: Decompiling in VS Code
For developers using Visual Studio Code, decompilation is seamless if you have the right extensions.
Using "Extension Pack for Java"
If you have the Extension Pack for Java (by Microsoft) installed, you already have decompilation capabilities.
- Open any
.classfile or a class inside a JAR library. - VS Code automatically decompiles it and displays the source.
The integration is so smooth that it feels like "Go to Definition." It handles variable name reconstruction quite well, making the code highly readable.
Method 2: IntelliJ IDEA (The Gold Standard)
If you use IntelliJ IDEA, you don’t need any extra plugins. It comes bundled with Fernflower, one of the most accurate decompiler engines in the world.
-
How to use: Simply double-click a
.classfile in the project tree, orCtrl + Click(Cmd + Clickon Mac) a method call from a library. - IntelliJ will instantly show the decompiled source with a small banner at the top identifying it as a decompiled file.
Method 3: Eclipse (Enhanced Class Decompiler)
By default, Eclipse shows a "Source not found" screen when opening a .class file. You can fix this by installing a plugin.
Steps for Enhanced Class Decompiler (ECD):
- Go to
Help > Eclipse Marketplace. - Search for "Enhanced Class Decompiler" and install it.
- Restart Eclipse.
- Set it as the default viewer under
Window > Preferences > General > Editors > File Associationsfor*.classfiles.
Specialized Tool: Recaf
If you need to analyze JAR files outside of an IDE, Recaf is the current industry favorite.
While tools like JD-GUI were popular in the past, they often struggle with modern Java syntax (Java 11+). Recaf is a modern bytecode editor that allows you to switch between multiple engines like CFR, Fernflower, and Procyon.
How to use Recaf:
- Download the JAR from the Recaf GitHub releases.
- Run it via CLI:
java -jar recaf-xxx.jar. - Drag and drop your JAR or
.classfile to start analyzing.
Command Line (CLI) Options
For automation or quick terminal checks, CLI tools are incredibly efficient.
1. The Standard javap
JDK comes with javap, a disassembler. It won't give you full Java source code, but it's perfect for checking method signatures and bytecode instructions.
javap -c MyClass.class
2. CFR Decompiler
CFR is an excellent standalone decompiler that supports the latest Java features (including Records and Sealed Classes).
java -jar cfr.jar MyClass.class
Legal and Practical Considerations
Before you start decompiling everything, keep two things in mind:
- Licensing & EULA: Many commercial libraries strictly forbid reverse engineering (including decompilation) in their Terms of Service. Always check the license before analyzing proprietary code.
- Code Integrity: Decompiled code might not re-compile perfectly. Treat it as a reference for logic rather than a direct replacement for source code.
Conclusion
Whether you prefer the seamless integration of IntelliJ, the lightweight feel of VS Code, or the power of CLI tools like CFR, decompilation is a "superpower" for Java developers. It turns "black box" libraries into open books, helping you debug faster and learn more.
Originally published at: [https://code-izumi.com/java/decompilation/]
Top comments (0)