105

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

  • [Look Inside Compiled Code with Java Bytecode Decompiler](https://blog.jetbrains.com/idea/2020/03/java-bytecode-decompiler/) -- This page introduced the plugin `Java Bytecode Decompiler` in 2020.03. By using this bundled plugin inside Intellij, it's pretty easy to decompile `.jar` to java code. – Scintiller Nov 03 '20 at 09:43

11 Answers11

94

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).

yan
  • 2,932
  • 1
  • 23
  • 25
  • 35
    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-decompiler.jar" org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler -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` – michele b Jul 11 '18 at 08:37
  • 4
    On Arch install: java -cp "/usr/share/idea/plugins/java-decompiler/lib/java-decompiler.jar" org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler -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 Sep 05 '18 at 14:38
  • 2
    This more brief command worked for me (based on these answers and the usage note): java -cp /java-decompiler/lib/java-decompiler.jar org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler -dgs=true – Marquee Oct 25 '18 at 15:24
  • 10
    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.ConsoleDecompiler -hdc=0 -dgs=1 -rsy=1 -rbr=1 -lit=1 -nls=1 -mpm=60 %*` – Vadzim Jan 17 '19 at 16:28
  • how could you read what intellij is using as params? i didn't find these info here https://github.com/JetBrains/intellij-community/tree/master/plugins/java-decompiler/engine – ahmed_khan_89 Aug 07 '23 at 16:07
51

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).

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
21
  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.

Pang
  • 9,564
  • 146
  • 81
  • 122
user2642646
  • 227
  • 2
  • 3
  • 2
    surprised this doesn't have even more votes because this is probably what most people actually are looking to do. – Nicholas DiPiazza Feb 22 '22 at 18:09
  • 2
    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 Apr 30 '22 at 02:54
  • Works thanks! Now how do I make it editable or move it to my actual intelliJ workspace? – Bishwas Mishra May 20 '22 at 16:53
  • you can right-click on jar file and choose the option "Add as Library..." - then IDE will allow to browse its content – Krzysztof Skrzynecki Jul 13 '22 at 14:01
  • strange, I did that with a Minecraft mod jar I want to examine (deobfuscated, of course) and there is no button to expand and explore the jar? – Twisted on STRIKE at1687989253 Oct 30 '22 at 02:02
  • it is in my libraries, but I'm not able to get the classes even, not the source code... – Betlista Jan 30 '23 at 10:38
4

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).

Bluexin
  • 41
  • 1
3

The decompiler of IntelliJ IDEA was not built with this kind of usage in mind. It is only meant to help programmers peek at the bytecode of the java classes that they are developing. For decompiling lots of class files of which you do not have source code, you will need some other java decompiler, which is specialized for this job, and most likely runs standalone, like jad, fernflower, etc.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
  • 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 –  Feb 08 '15 at 00:31
3

Try

https://github.com/fesh0r/fernflower

Download jar from

http://files.minecraftforge.net/maven/net/minecraftforge/fernflower/

Command :

java -jar fernflower.jar -hes=0 -hdc=0 C:\binary  C:\source

Place your jar file in folder C:\binary and source will be extracted and packed in a jar inside C:\source.

Enjoy!

Prasobh.Kollattu
  • 1,665
  • 1
  • 22
  • 32
2

Some time ago I used JAD (JAva Decompiler) to achieve this - I do not think IntelliJ's decompiler was incorporated with exporting in mind. It is more of a tool to help look through classes where sources are not available.

JAD is still available for download, but I do not think anyone maintains it anymore: http://varaneckas.com/jad/

There were numerous plugins for it, namely Jadclipse (you guessed it, a way to use JAD in Eclipse - see decompiled classes where code is not available :)).

theadam
  • 3,961
  • 4
  • 25
  • 41
  • 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 ( –  Feb 08 '15 at 06:59
1

I use JD-GUI for extract all decompiled java classes to java files.

1

Someone had gave good answers. I made another instruction clue step by step. First, open your studio and search. You can find the decompier is Fernflower.

enter image description here

Second, we can find it in the plugins directory.

/Applications/Android Studio.app/Contents/plugins/java-decompiler/lib/java-decompiler.jar

Third, run it, you will get the usage

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

Usage: java -jar fernflower.jar [-<option>=<value>]* [<source>]+ <destination>
Example: java -jar fernflower.jar -dgs=true c:\my\source\ c:\my.jar d:\decompiled\

Finally, The studio's nest options for decompiler list as follows according IdeaDecompiler.kt

-hdc=0 -dgs=1 -rsy=1 -rbr=1 -lit=1 -nls=1 -mpm=60 -lac=1

IFernflowerPreferences.HIDE_DEFAULT_CONSTRUCTOR to "0",
        IFernflowerPreferences.DECOMPILE_GENERIC_SIGNATURES to "1",
        IFernflowerPreferences.REMOVE_SYNTHETIC to "1",
        IFernflowerPreferences.REMOVE_BRIDGE to "1",
        IFernflowerPreferences.LITERALS_AS_IS to "1",
        IFernflowerPreferences.NEW_LINE_SEPARATOR to "1",
        **IFernflowerPreferences.BANNER to BANNER,**
        IFernflowerPreferences.MAX_PROCESSING_METHOD to 60,
        **IFernflowerPreferences.INDENT_STRING to indent,**
        **IFernflowerPreferences.IGNORE_INVALID_BYTECODE to "1",**
        IFernflowerPreferences.VERIFY_ANONYMOUS_CLASSES to "1",
         **IFernflowerPreferences.UNIT_TEST_MODE to if (ApplicationManager.getApplication().isUnitTestMode) "1" else "0")**

I cant find the sutialbe option for the asterisk items.

Hope these steps will make the question clear.

Victor Choy
  • 4,006
  • 28
  • 35
1

2022 UPDATE

Like any experienced developer who's being honest, I have to admit it -- I'm lazy. More is less and less is better when it comes to typing. The answers from @naXa and @yan are fine, except for having to open a terminal and type :-(

To decompile a jar right from within intellij, create a reusable JAR Application run configuration:

  1. Copy java-decompiler.jar to a folder in your project (I used lib). Get from:: Windows: C:\Program Files\Intellij\plugins\java-decompiler\lib; or from github (documentation is here)
  2. Copy source jar or class files to same folder
  3. Create a run configuration with settings like these:
  • Path to JAR: browse to java-decompiler.jar
  • Program arguments: -hdc=0 -dgs=1 -rsy=1 -lit=1 your.jar lib
  • Working directory: lib
  • JRE: 11

In this case, your.jar is the source and lib is the output directory. Save it, run it, and watch it do its thing :-)

Need to do it again with different class files or jars? Duplicate the run config and update Program arguments.

cb4
  • 6,689
  • 7
  • 45
  • 57
  • Where does this java-decompiler.jar come from? – Dave Ankin Jul 01 '22 at 17:57
  • 1
    @DaveAnkin Updated answer with two locations. – cb4 Jul 01 '22 at 20:18
  • 1
    Quick vineflower answer: Get the complied code that uses Java 17 here: https://github.com/Vineflower/vineflower/releases. Use command: java -jar vineflower.jar -dgs=1 c:\Temp\binary\library.jar c:\Temp\souce – splashout Aug 03 '23 at 21:43
0

---- package jar (java) jar -cvfM0 test.jar

  • ---Decompile jar(class)>>>>>>>>>jar(java) java -cp "C:\Program Files\JetBrains\IntelliJ IDEA 2020.1\plugins\java-decompiler\lib\java-decompiler.jar" org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler -dgs=true C:\ Users\luo\Desktop\sf1\pxks\test.jar C:\Users\luo\Desktop\sf1

Unzip manually

Or use the following command to decompress

find the path cd..

decompress jar -xvf test.jar

Reference http://t.csdn.cn/3YLK4

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 14 '23 at 09:36