How do I decompile Java class files?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Decompiling Java class files is an important task for many developers and security analysts who need to examine the content of compiled Java applications. Java programs are typically compiled into bytecode, which runs on the Java Virtual Machine (JVM). This bytecode is stored in .class files. Decompiling is the process of converting bytecode back into java source code, albeit often not the exact original code, especially when the original code was obfuscated or minimized.
Why Decompile?
Decompiling Java class files allows developers to:
- Understand how a Java application works, which is particularly useful when the source code is not available.
- Debug in cases where the source code has been lost or is no longer accessible.
- Analyze the application for security vulnerabilities.
- Check for malicious code in third-party proprietary libraries.
How Java Decompilation Works
Java bytecode is a set of instructions that the JVM understands, but it is not immediately human-readable. Decompilation translates these instructions back into Java code by reconstructing the program logic and structure from bytecode sequence. However, certain information such as comments, variable names, and method names (if they were changed during obfuscation) might not be fully recoverable.
Tools for Decompiling Java Class Files
Various tools are available for decompiling Java class files. Below are some of the most widely used:
- JD-GUI: This is a standalone graphical utility that displays source codes of ".class" files, and you can browse the reconstructed source code to analyze its contents.
- JAD: Java Decompiler (JAD) is one of the first decompilers, but hasn't been maintained for several years. It's command-line-based and works fast, but might not handle newer versions of Java well.
- Procyon: A newer decompiler that handles language enhancements from later Java versions beautifully, including lambda expressions and more.
- CFR: Another modern decompiler that aims to provide good output even when dealing with trickier constructs like loops and lambdas.
Step-by-Step Process
To decompile a Java .class file using JD-GUI, follow these steps:
- Download and open JD-GUI.
- Click on
File > Open File...and select the.classfiles or JAR file containing the.classfiles. - Browse the class files in the left-hand panel and view decompiled source code in the right-hand panel.
- Optionally, you can save the source code by clicking
File > Save All Sourcesto save all decompiled classes as.javafiles.
Table of Popular Java Decompilers
| Decompiler | GUI Support | Maintained | Java Version Compatibility |
| JD-GUI | Yes | Yes | Up to Java 15 |
| JAD | No | No | Up to Java 1.4 |
| Procyon | No | Yes | Up to Java 16 |
| CFR | No | Yes | Up to Java 16 |
Legal and Ethical Considerations
While decompiling can be incredibly useful, it's important to consider the legal and ethical implications. Decompiling proprietary software without permission can violate copyright laws and software license agreements. Always ensure that you have the right to decompile the code in question, for example, for educational purposes, for security analysis in your own code, or with explicit permission from the copyright holder.
Conclusion
Decompiling Java class files can provide significant insights into the operation and structure of applications. By selecting the appropriate tool and following guidelines on legal usage, you can reverse-engineer compiled Java code effectively, aiding in everything from educational purposes to security testing. Just be cautious of the legal landscape regarding decompilation to avoid potential pitfalls.
Related reading
- How do I define a method which takes a lambda as a parameter in Java 8?
- How Do I Document Packages in Java?
- How do I enable index downloads in Eclipse for Maven dependency search?
- How do I exclude all instances of a transitive dependency when using Gradle?
- How do I find out what keystore my JVM is using?
- How do I find the caller of a method using stacktrace or reflection?
- How do I find where JDK is installed on my windows machine?
- How do I fix a NoSuchMethodError?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.