Extract source code from .jar file - java

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

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

You can extract a jar file with the command :
jar xf filename.jar
References : Oracle's JAR documentation

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 :
Download JAD from http://techieme.in/resources-needed/ and save it at any
location on your system.
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.
Click on File menu and select save jar sources.
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

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.

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.

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
// download jar procyon from https://drive.google.com/file/d/1yC2gJhmLoyE8royCph5dLEncgkNZXj58/view?usp=sharing
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 :
Download JD-GUI from http://java-decompiler.github.io/ and save it
at any location on your system.
Drag and drop the jar or open .jar file for which you want the
sources on the JD.
Java Decompiler will open with all the package structure in a tree
format.
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:-
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

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

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.

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

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

Related

Create a jar file using compiled class files and an existing MANIFEST.MF file

Is it possible to take existing .class files and a MANIFEST.MF to create a jar file?
Is there a library that can create a "valid" jar-file? I tried it manually and it didn't work (using 7zip).
ERROR: "Invalid or corrupt jar file"
If everything has been compiled before, it should (in my understanding) theoretically work, if you create a new zip file, put all the files in it in the original structure and then rename it to "jar".
My idea is to program something like this with java code. A solution where I could add a file to an existing jar, would also be ok.
If you're interested in why I want to use this, look at my initial question: Compile javacode out of a running java accpilaction - on a system that hasn't JDK installed
Well Jar -cf
Try the jar command in $JAVA_HOME/bin
$JAVA_HOME is the path to you JRE/JDK installation

Convert Jython scripts to .jar file

I have been trying to convert jython scripts to .jar file. I followed,
Distributing my Python scripts as JAR files with Jython?
The link above and also the Jython wiki link shown inside it use a command line script called, "zip". Its like => zip myapp.jar Lib
and so. I am on windows machine, so I couldn't find any script as "zip" on my command line, may be its a Linux script. Help me to do it in windows machine
Second is I have few external .jar's that are used in my jython scripts, for this the above link says to use manifest. But I want to package all external jars into single myapp.jar(for example) file to be running. Just like java -jar myapp.jar, so is there any way to package external jars also?
So please help me resolve this
You can use jar command to add files to .jar archive. Just like:
jar uvf jythonlib.jar Lib
Use jar to see help with options and examples of usage.
As you can see, the code zip -r jythonlib.jar Lib and zip myapp.jar Lib/showobjs.py, just add files to a jar archive. The zip program is a specific tool to do this, but it is not the only one. On windows, I would recommend using 7-zip to create the jar. Rather than zip -r jythonlib.jar Lib, open a new 7-zip window, create a new archive (zip protocol) called jythonlib.jar and add the folder called Lib which resides in $JYHTON_HOME. Then continue with the guide and when you get to zip myapp.jar Lib/showobjs.py, simply drag and drop the file called showobjs.py into the existing 7-zip window. This should add the file to the archive.

Recreate project from .jar file

Is there any chance, to recreate a (netbeans-)project from a .jar file?
Becuase I trusted some silly cryption-software that corrupt's my file.
Is their any suggestion or solution?
I already extracted the .jar file with terminal commands 'jar xf MyJarFile.jar'.
But I can't read the code in it...
You could decompile the jar into the sources and then create a new project and drag all the sources into your src folder. I like using JD-GUI for decompiling .jars: http://java.decompiler.free.fr/?q=jdgui
I also recommend using JD-GUI, it helped me a lot when my simple text editor corrupted my .java files and I was able to convert the classes back into source using it.
delete the $ files first, then Jad is the best, jdgui makes errors.

To view the content of .jar file

I do have a .jar file and i need to view the content of the jar files.
Is there any decoder available for decoding the jar file.
I have the solaris environment also and i need to know what is the
Use the jar command line, i.e.
jar tf jarfile.jar
unzip it. It's just a zip file.
Just google jd-gui
Hopefully this works for you
*.jar are archived files.
Try extracting it with an Archiver!(similar to WinRAR on windows)
The "official" utility is called "jar" and should be installed if a Java environment is installed. It is a command line utility that works similar to the UNIX tool "tar".
But as Mick Sear mentioned, "unzip" will also work.
JAR files are packaged as zips, so any tools you use to open zip files should work.

Modifying a jar file

I have a jar file which is used in html file as applet. I want to modify the content of the jar file and to rebuild the jar file so that the html will work fine with the new jar file. How can i do this??
I already tried unzipping using 7zip nad modified the source and created the new jar. But when i use it in html it shows some java.lang.Classnotfound error
You can unjar or rejar the classes and source files as you wish.
unjar
jar -xvf abc.jar
jar
jar cf abc.jar input-files
http://docs.oracle.com/javase/tutorial/deployment/jar/build.html
Make the changes in the code (.java files), recompile to get the .class files. Then simply replace the old .class files in the jar with the new ones. I usually use WinZip, but you can use whatever app that can handle .Zip files. It should just work.
I've faced cases where the launcher of the app uses some sort of verification and checks for this kind of changes. I had to use a new launch script. This doesn't seem to be your case though.
This is surely possible from the command line. Use the u option for jar
From the Java Tutorials:
jar uf jar-file input-file(s)
"Any files already in the archive having the same pathname as a file being added will be overwritten."
See Updating a JAR File
A brief test shows this quickly updates changes apart from trying to delete the file.
I haven't seen this answer on other threads about modifying jar files, and many, marked as duplicates, suggest there is no alternative but to remake the jar completely. Please correct if wrong.
JARs are just ZIP files, use whatever utility you like and edit away!
Disclaimer: When reverse engineering any code be sure that you are staying within the limits of the law and adhering to the license of that code.
Follow the instructions above to unpack the JAR.
Find the original source of the JAR (perhaps its on SourceForge) and download the source, modify the source, and rebuild your own JAR.
You can also decompile the class files in the JAR. This is a rather advanced process and has a lot of "gotchas".

Categories

Resources