.JAR and .Class to Java decompiler
Until recently, you needed to use a Java decompiler and all of them were either unstable, obsolete, unfinished, or in the best case all of the above. And, if not, then they were commercial. The obsoleteness was typically proved by the fact that they can only decompile JDK 1.3 bytecode.
The only so-so working solution was to take the .class file and pre-process it, so it becomes JDK 1.3 compatible, and then run Jad over it (one of those older, but better decompilers).
But recently, a new wave of decompilers has forayed onto the market: Procyon, CFR, JD, Fernflower, Krakatau, Candle.
Here’s a list of decompilers presented on this site:
This free and open-source decompiler is available here: http://www.benf.org/other/cfr/
Author: Lee Benfield
- Java 7: String switches
- Java 8: lambdas
- Java 9: modules
- Java 11: dynamic constants
- Java 12: Kotlin style «switch expressions»
- Java 14: ‘instance of’ pattern match and ‘Record types’
free for non-commercial use only, http://jd.benow.ca/
Author: Emmanuel Dupuy
Updated in 2015. Has its own visual interface and plugins to Eclipse and IntelliJ . Written in C++, so very fast. Supports Java 5.
Procyon
- Enum declarations
- Enum and String switch statements
- Local classes (both anonymous and named)
- Annotations
- Java 8 Lambdas and method references (i.e., the :: operator).
Fernflower
Updated in 2015. Very promising analytical Java decompiler, now becomes an integral part of IntelliJ 14. (https://github.com/JetBrains/intellij-community/tree/master/plugins/java-decompiler)
Supports Java up to version 6 (Annotations, generics, enums)
given here only for historical reason. Free, no source-code available, jad download mirror
Author: Pavel Kouznetsov
Probably, this is the most popular Java decompiler, but primarily of this age only. Written in C++, so very fast.
Outdated, unsupported and does not decompile correctly Java 5 and later.
Decompiling Java Class Files On the Command-Line
Table of Contents
Sometimes you come across compiled Java/JVM class files that you need to decompile. It’s incredibly convenient when this occurs when this happens while in the midst of development, in your IDE, but sometimes it’s when you’re SSH’d into a machine, and it’s a little more awkward, or if you don’t have an IDE installed.
Fortunately, the Fernflower decompiler that IntelliJ uses under the hood is Open Source and has a powerful command-line interface that can be used to decompile classes.
Getting Fernflower
Before we can run this, you need to have access to the Fernflower JAR file.
IntelliJ
If you have IntelliJ installed, you can find it present in the plugins/java-decompiler/lib/java-decompiler.jar path within your IntelliJ installation.
The version distributed unfortunately doesn’t have a Main-Class attribute in the JAR, so we need to provide it when executing it:
Building from source
As the JAR isn’t (officially) published to any Maven repositories, if you don’t have IntelliJ installed, it’s worthwhile building it yourself from source.
You can either build it from the official IntelliJ repo or may find it more convenient to use the unofficial mirror of just the decompiler.
This version includes the Main-Class attribute so you can execute it via:
Running
Now we have our JAR, and the prefix required to execute it, we can then decompile the class file(s) to a directory, for instance:
And now we can open up FullInstructionSequence.java in our editor to see the decompiled code!
There are a lot of options to tune the decompilation process, so it’s worth having a look at the project README to see which are best for usage.
Written by
Jamie Tanna on Mon, 20 Dec 2021 20:49:57 UTC , and last updated on Mon, 14 Feb 2022 11:24:33 UTC .
Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.
Has this content helped you? Did it solve that difficult-to-resolve issue you’ve been chasing for weeks? Or has it taught you something new you’ll be able to re-use daily?
Please consider supporting me so I can continue to create content like this!
This post was filed under articles.
Interactions with this post
Below you can find the interactions that this page has had using WebMention.
Have you written a response to this post? Let me know the URL:
Do you not have a website set up with WebMention capabilities? You can use Comment Parade.
Want to get in touch? Drop me an email at hi@jamietanna.co.uk. I try to get back to everything, if I don’t reply in a couple of weeks, feel free to give me a nudge!
You can also check out a list of all the places /elsewhere you can find me.
How to decompile class file in Java and Eclipse — Javap command example
How to decompile Class file in Eclipse IDE
Once you have JD-Eclipse installed and running, you probably need to restart Eclipse once you installed the plug-in. You are ready to decompile Java class file from Eclipse. In order to decompile class file, just open any of your Java projects and go to Maven dependencies of libraries to see the jar files included in your project.
How to decompile class file in Java – javap command example
/java javap
No classes were specified on the command line. Try -help.
/java cat Hello.java
public class Hello <
private String version= «1.2» ;
public static void main ( String args []) <
System. out . println ( «Running Hello» ) ;
>
>
/java javap Hello
Compiled from «Hello.java»
public class Hello extends java. lang .Object <
public Hello () ;
public static void main ( java. lang .String []) ;
>
/java javap — private Hello
Compiled from «Hello.java»
public class Hello extends java. lang .Object <
private java. lang .String version ;
public Hello () ;
public static void main ( java. lang .String []) ;
>
How to see bytecode from .class file
/java javap -c Hello
Compiled from «Hello.java»
public class Hello extends java. lang .Object <
public Hello () ;
Code:
0 : aload_0
1 : invokespecial # 1 ; //Method java/lang/Object.»»:()V
4 : aload_0
5 : ldc # 2 ; //String 1.2
7 : putfield # 3 ; //Field version:Ljava/lang/String;
10 : return
public static void main ( java. lang .String []) ;
Code:
0 : getstatic # 4 ; //Field java/lang/System.out:Ljava/io/PrintStream;
3 : ldc # 5 ; //String Running Hello
5 : invokevirtual # 6 ; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
8 : return
5 comments :
JD-Eclipse is certainly useful, but it didn't go quite far enough. You need the decompiled code to be aligned with its original line numbers, so you can set breakpoints in the decompiled code. That's where http://mchr3k.github.com/jdeclipse-realign/ comes in.
That is true David. I have provided a step by step instructions here to install JD-Eclipse, hopefully that will be helpful to some of our fellow programmers.
What is the best ways to decompile Java class files from command prompt? Examples given here doesn't show how to get full decompiled code, rather than byte code or just method and fields name. Precisely, I have a Java class file, which doesn't use any library and I want to see the code. I don't use IDE for such purpose, so any plain old command will help. Thanks
There are lots of free and open source Java decompilers are out there, you can use any of them to decompile java class files from command prompt. Here is a list of some popular Java class file decompilers :
Procyon (open-source and actively developed)
Krakatau (open-source and actively developed)
CFR
JAD
DJ Java Decompiler
Mocha
JAD Decompiler
Soot
Jdec
Java Decompiler
As a developer who splits his time between the .NET and Java platforms, I have been surprised and dismayed by the lackluster selection of decompilers in the Java ecosystem. Jad (no longer maintained, closed source) and JD-GUI (GPL3) are pretty decent choices, but the former does not support Java 5+ language features, and the latter tends to barf on code emitted by my LINQ/DLR tree compiler.
To address the situation, I recently started developing a decompiler myself, inspired by (and borrowed heavily from) ILSpy and Mono.Cecil.
While still incomplete, my own tests seem to indicate that the Procyon decompiler can generally hold its own against the other leading Java decompilers out there. There are, however, some known issues.
- Classes compiled with Eclipse (or anything other than javac ) may produce less than optimal results, especially for constructs like String-based switch statements. Up until now, I have mostly tested classes compiled with Javac. I will work to improve support for other compilers as time goes on.
Why should I care?
The Procyon decompiler handles language enhancements from Java 5 and beyond that most other decompilers don’t. It also excels in areas where others fall short. Procyon in particular does well with:
- Enum declarations
- Enum and String switch statements (only tested against javac 1.7 so far)
- Local classes (both anonymous and named)
- Annotations
- Java 8 Lambdas and method references (i.e., the :: operator).
I’ve posted some simple Decompiler Output Comparison comparing original source, decompiled output, and JD-GUI output.
Java 7 is required to run. Unfortunately, I do not yet have a slick GUI front-end like JD-GUI (but third-party front-ends do exist—see below!). I do, however, offer color-coded output for consoles supporting ANSI/xterm-256. I also offer three output modes:
- Java (default)
- Raw Bytecode (similar to javap, but prettier; run with -r )
- Bytecode AST (an intermediate representation; run with -b , add -u for unoptimized)
Note that color-coded output requires an ANSI-compatible console. Unfortunately, this rules out the Windows command prompt. To get color-coded output on Windows, I recommend using a terminal environment like MobaXterm. If ANSI detection fails for whatever reason (as it does with MobaXterm), you can force it on by running with -DAnsi=true .
The main class (entry point) is com.strobel.decompiler.DecompilerDriver . It’s also the entry class for decompiler.jar (available under Downloads). You can pass in one or more types to be processed. At the moment, all output goes to System.out . I will probably add file-based output in the future. To call the public API externally, use the helper class com.strobel.decompiler.Decompiler .
The input types can be fully-qualified names in dotted or binary form (e.g., java.lang.String or java/lang/String ) or relative/absolute file paths ( path/to/MyClass.class or C:\src\path\to\MyClass.class ) or even whole jar files. If you pass in a type name, it will attempt to load it out of the user or bootstrap classpath. If you have trouble getting it to locate classes from jars or directories in your CLASSPATH environment variable, try running the main class directly instead of running with -jar .
I still have a remarkably flimsy grasp of how all this classpath business works in Java, so if someone would like to educate me (or submit a pull request), be my guest :).
Assuming that you have renamed the download file from procyon-decompiler-x.x.xx.jar to decompiler.jar, the following are some common options.
Show help/usage information and exit.
Decompile a single class to the console.
Decompile a whole jar to a directory.
The command-line decompiler is simply a front-end; the decompiler itself is a framework that can be integrated into other applications and called programmatically. To learn more, read this.
Don’t want to use the command line? Try one of these GUI front-ends for Procyon:
SecureTeam Java Decompiler
A JavaFX-based decompiler front-end with fast and convenient code navigation. Download it, or launch it directly from your browser.
Bytecode Viewer is an open source Java decompilation, disassembly, and debugging suite by @Konloch. It can produce decompiled sources from several modern Java decompilers, including Procyon, CFR, and FernFlower.
Helios is similar to Bytecode Viewer. But is a completely new and independent project, which uses SWT instead of Swing.
Send me your feedback!
If you are using my decompiler, I would love to hear from you. Drop me a line (email / twitter) and tell me about your experiences—did it work or not work? Did you love it or hate it?
There are other Java Decompiler projects underway that you should check out: