DEV Community

ライフポータル
ライフポータル

Posted on • Originally published at code-izumi.com

Java Decompilation Guide: How to Recover Source Code in VS Code, Eclipse, and Beyond

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 .java files and need to recover them from compiled .class files."

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?

  1. Auditing Libraries: Understanding the internal logic of a JAR file when documentation is lacking.
  2. Source Code Recovery: Recovering logic from a compiled build after accidental data loss.
  3. Debugging & Verification: Ensuring the .class file 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.

  1. Open any .class file or a class inside a JAR library.
  2. 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 .class file in the project tree, or Ctrl + Click (Cmd + Click on 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):

  1. Go to Help > Eclipse Marketplace.
  2. Search for "Enhanced Class Decompiler" and install it.
  3. Restart Eclipse.
  4. Set it as the default viewer under Window > Preferences > General > Editors > File Associations for *.class files.

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:

  1. Download the JAR from the Recaf GitHub releases.
  2. Run it via CLI: java -jar recaf-xxx.jar.
  3. Drag and drop your JAR or .class file 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Legal and Practical Considerations

Before you start decompiling everything, keep two things in mind:

  1. Licensing & EULA: Many commercial libraries strictly forbid reverse engineering (including decompilation) in their Terms of Service. Always check the license before analyzing proprietary code.
  2. 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)