[java] Extract source code from .jar file

Is there a way to extract the source code from an executable .jar file (Java ME)?

This question is related to java jar java-me decompiling

The answer is


-Covert .jar file to .zip (In windows just change the extension) -Unzip the .zip folder -You will get complete .java files


Your JAR may contain source and javadoc, in which case you can simply use jar xf my.jar to extract them.

Otherwise you can use a decompiler as mentioned in adarshr's answer:

Use JD GUI. Open the application, drag and drop your JAR file into it.


AndroChef Java Decompiler produces very good code that you can use directly in your projects...


suppose your JAR file is in C:\Documents and Settings\mmeher\Desktop\jar and the JAR file name is xx.jar, then write the below two commands in command prompt:

1> cd C:\Documents and Settings\mmeher\Desktop\jar

2> jar xf xx.jar


Use JD GUI. Open the application, drag and drop your JAR file into it.


I believe this can be done very easily. You can always extract the source files (Java files) of a jar file into a zip.

Steps to get sources of a jar file as a zip :

  1. Download JAD from http://techieme.in/resources-needed/ and save it at any
    location on your system.
  2. Drag and drop the jar for which you want the sources on the JAD. 3 JAD UI will open with all the package structure in a tree format.
  3. Click on File menu and select save jar sources.
  4. It will save the sources as a zip with the same name as the jar.

Hope this helps.

The link is dead due to some reason so adding the link from where you can download the JDGUI


Above tools extract the jar. Also there are certain other tools and commands to extract the jar. But AFAIK you cant get the java code in case code has been obfuscated.


Do the following on your linux box where java works (if u like the terminal way of doing things)

cd ~
mkdir decompiled_code && cd decompiled_code
wget https://bitbucket.org/mstrobel/procyon/downloads/procyon-decompiler-0.5.36.jar
java -jar procyon-decompiler-0.5.36.jar /Path/to/your/jar -o .

NOTE : as @Richard commented "this may be illegal depending on whether you own the copyright to the jar, the country you live in and your purpose for doing it."


Steps to get sources of a jar file as a zip :

  1. Download JD-GUI from http://java-decompiler.github.io/ and save it at any location on your system.

  2. Drag and drop the jar or open .jar file for which you want the sources on the JD.

  3. Java Decompiler will open with all the package structure in a tree format.

  4. Click on File menu and select save jar sources. It will save the sources as a zip with the same name as the jar.

Example:-

enter image description here

We can use Eclipse IDE for Java EE Developers as well for update/extract code if require.

From eclipse chose Import Jar and then select jar which you need. Follow instruction as per image below

enter image description here

enter image description here


I know it's an old question Still thought it would help someone

1) Go to your jar file's folder.

2) change it's extension to .zip.

3) You are good to go and can easily extract it by just double clicking it.

Note: I tested this in MAC, it works. Hopefully it will work on windows too.


You can extract a jar file with the command :

jar xf filename.jar

References : Oracle's JAR documentation


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to jar

Running JAR file on Windows 10 Add jars to a Spark Job - spark-submit Deploying Maven project throws java.util.zip.ZipException: invalid LOC header (bad signature) java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonFactory Maven Error: Could not find or load main class Where can I download mysql jdbc jar from? Access restriction: The type 'Application' is not API (restriction on required library rt.jar) Create aar file in Android Studio Checking Maven Version Creating runnable JAR with Gradle

Examples related to java-me

Extract source code from .jar file Difference between volatile and synchronized in Java Difference between Java SE/EE/ME? How to get access to raw resources that I put in res folder? J2ME/Android/BlackBerry - driving directions, route between two locations Convert a JSON string to object in Java ME? How Long Does it Take to Learn Java for a Complete Newbie? How do I convert between ISO-8859-1 and UTF-8 in Java?

Examples related to decompiling

How to decompile to java files intellij idea How to decompile an APK or DEX file on Android platform? How to change already compiled .class file without decompile? Reverse engineering from an APK file to a project Decompile an APK, modify it and then recompile it Is it possible to decompile a compiled .pyc file into a .py file? Extract source code from .jar file Is there a way to get the source code from an APK file? Is it possible to decompile an Android .apk file? How can I protect my .NET assemblies from decompilation?