How to decompile to java files intellij idea

164,096

Solution 1

To use the IntelliJ Java decompiler from the command line for a jar package follow the instructions provided here: https://github.com/JetBrains/intellij-community/tree/master/plugins/java-decompiler/engine

Solution 2

As of August 2017 and IntelliJ V2017.2, the accepted answer does not seem to be entirely accurate anymore: there is no fernflower.jar to use.

The jar file is called java-decompiler.jar and does not include a main manifest... Instead you can use the following command (from a Mac install):

java -cp "/Applications/IntelliJ IDEA.app/Contents/plugins/java-decompiler/lib/java-decompiler.jar" org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler

(you will get the wrong Usage command, but it does work).

Solution 3

Follow instructions for IntelliJ JD plugin. Or see an excerpt from the instructions below.

java -jar fernflower.jar [<source>]+ <destination>

+ means 1 or more times
<source>: file or directory with files to be decompiled. Directories are recursively scanned. Allowed file extensions are class, zip and jar.
<destination>: destination directory

Example:

java -jar fernflower.jar -hdc=0 -dgs=1 -rsy=1 -lit=1 c:\Temp\binary\ -e=c:\Java\rt.jar c:\Temp\source\

Be aware that if you pass it a ".jar" file for the source, it will create another ".jar" file in the destination, however, within the new ".jar" file, the files will be .java instead of .class files (it doesn't explode the jar).


UPDATE

People ask me: How do I get the fernflower.jar?

If you have any IntelliJ product installed, chances are that you already have the Fernflower decompiler on your computer. IntelliJ IDEA comes with Java Bytecode Decompiler plugin (bundled) which is a modern extension of Fernflower.

  1. Locate the file in ${IntelliJ_INSTALL_DIR}\plugins\java-decompiler\lib\java-decompiler.jar (example: C:\Program Files\JetBrains\IntelliJ IDEA 2018\plugins\java-decompiler\lib).
  2. Copy it somewhere and rename to fernflower.jar (optional).
  3. This JAR is not executable, so we can't run it using java -jar. However something like this works:

    java -cp fernflower.jar org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler [<source>]+ <destination>
    

    org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler is the class that contains the main method to run the decompiler.

    Example:

    mkdir output_src
    java -cp fernflower.jar org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler -hdc=0 -dgs=1 -rsy=1 -lit=1 ./input.jar ./output_src
    

If you don't have IntelliJ products installed, either download it now (available on jetbrains.com) or make your own decompiler executable from sources (available on Github).

Solution 4

  1. Open an existing project or create a new one.

  2. Go to Project structure settings > Libraries. Add the jar you want to decompile in libraries by clicking the + symbol.

  3. Go to the Project tool window shown on the left. Search for jar name that you added in the previous step. Navigate to the desired class or package.

You can see the decompiled java files for that jar.

Solution 5

You could use one of these (you can both use them online or download them, there is some info about each of them) : http://www.javadecompilers.com/

The one IntelliJ IDEA uses is fernflower, but it can't handle recent things - like String/Enum switches, generics (didn't test this one personally, only read about it), ... I just tried cfr from the above website and the result was the same as with the built-in decompiler (except for the Enum switch I had in my class).

Share:
164,096
Admin
Author by

Admin

Updated on August 04, 2021

Comments

  • Admin
    Admin almost 3 years

    IDEA has a great built-in feature - decompiler. It works great.I can copy source code, but I cannot find option to extract all decompiled java classes to java files.
    This project has a lot of java classes and packages, so I will be to long to copy this manually.
    Does anyone know how to extract to java source files. Thx

  • Admin
    Admin over 9 years
    but I have copied manually code and it works. Maybe there is any way to do this automaticly or write script to copy from decompiled archive to the project
  • Admin
    Admin over 9 years
    Intellij decompiler works great in comparison with other tools, but sometimes it cannot decompile lamdas, so it lefts invokedynamic, but I have tried cfr , and it does it well, but other code not as good as intellij , so I have to copy all code manually (
  • asmaier
    asmaier over 7 years
    How do I get the fernflower.jar ?
  • Make42
    Make42 about 7 years
    @naXa: Really? Like how many actually useful .jars did turn up, when googleing? Not so many, actually...
  • naXa stands with Ukraine
    naXa stands with Ukraine about 7 years
    @Make42 I didn't get it. Well, fernflower is an open source project. It means everyone can package a jar for himself.
  • michele b
    michele b almost 6 years
    Merging this answer with the top comment from the accepted answer, the command to launch to use the same params that IDEA uses by default is java -cp "/Applications/IntelliJ IDEA.app/Contents/plugins/java-decompiler/lib/java-decompile‌​r.jar" org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompi‌​ler -hdc=0 -dgs=1 -rsy=1 -rbr=1 -lit=1 -nls=1 -mpm=60 /absolute/path/to/src1 /absolute/path/to/src2 /absolute/path/to/dst
  • mbruns42
    mbruns42 over 5 years
    On Arch install: java -cp "/usr/share/idea/plugins/java-decompiler/lib/java-decompiler‌​.jar" org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompi‌​ler -hdc=0 -dgs=1 -rsy=1 -rbr=1 -lit=1 -nls=1 -mpm=60 /absolute/path/to/src1 /absolute/path/to/src2 /absolute/path/to/dst
  • Marquee
    Marquee over 5 years
    This more brief command worked for me (based on these answers and the usage note): java -cp <path>/java-decompiler/lib/java-decompiler.jar org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompi‌​ler -dgs=true <target.jar> <dest>
  • Vadzim
    Vadzim over 5 years
    Windows decompile.cmd scr1 src2 dst: java -cp "%ProgramFiles%\JetBrains\IntelliJ IDEA 2018.3\plugins\java-decompiler\lib\java-decompiler.jar" org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompi‌​ler -hdc=0 -dgs=1 -rsy=1 -rbr=1 -lit=1 -nls=1 -mpm=60 %*
  • Freiheit
    Freiheit about 5 years
    Note that this produces sources JARs which would then have to be unpacked to view the decompiled Java files. You can unpack them with a one liner in bash find ./yoursourcesdirectory -name '*.jar' -exec sh -c 'unzip -d "${1%.*}" "$1"' _ {} \;
  • 3xCh1_23
    3xCh1_23 over 4 years
    Doesn't install on linux
  • Matteo
    Matteo over 3 years
    I did install the plugin, but I cannot find anything...
  • naXa stands with Ukraine
    naXa stands with Ukraine over 3 years
    @Madeo where did you look?
  • Matteo
    Matteo over 3 years
    in the plugins folder of Intellij, but it's ok, I have used JDGUI.
  • Nicholas DiPiazza
    Nicholas DiPiazza about 2 years
    surprised this doesn't have even more votes because this is probably what most people actually are looking to do.
  • cb4
    cb4 about 2 years
    Downvoted - doesn't answer the question of getting to source files. @NicholasDiPiazza Not necessarily -- it only allows you to browse and only one file at a time. To get source, you would have to copy/paste from the editor window into a new .java file. Not fun when you have 50 or 100 class files you need the source for.
  • cb4
    cb4 about 2 years
    Doesn't say how to do it in intellij.
  • Loren
    Loren about 2 years
    jar url says not found
  • Bishwas Mishra
    Bishwas Mishra almost 2 years
    Works thanks! Now how do I make it editable or move it to my actual intelliJ workspace?