- Java – Decompile JAR files and view Source Code
- Extracting the Contents of a JAR File
- Download
- How to Extract the Source Code of a Java Application ☕
- Procedure
- Install JD-core-java
- Decompile the JAR files
- Additional links
- How to read a class file from an extracted jar file?
- 2 Answers 2
- Extract the Contents of .zip / .jar files programmatically
Java – Decompile JAR files and view Source Code
In software terms, JAR (Java Archive) is a package file format typically used to aggregate many Java class files and associated metadata and resources (text, images, etc.) into one file to distribute application software or libraries on the Java platform.
Typically, the contents of any JAR file can be viewed by extracting the JAR file with the help of Tools like 7zip, WinZIP or WinRAR. Once You have successfully extracted the contents of the JAR file, you will get a lot of files with file extension «.class» and these class files are not in human readable format.
So, to overcome this problem and to view the original JAVA source code hidden inside these class files stored inside the JAR file container, we use a tool called JD-GUI.
JD stands for «Java Decompiler«. It is a standalone graphical utility that displays Java source codes of .class files. You can browse the reconstructed source code with the JD-GUI for instant access to methods and fields.
First, we need to download the JD-GUI software:
And You get the home screen like the following:
Click on File — > Open and choose any JAR file of your choice:
After opening JAR file, you will get a window like this:
Now click on the + icon to expand and view the contents of the packages.
NOTE: «.class» files are found inside these packages.
Here, our package is «bruteCUI» and then we get a list of «.class» files. Now click on any class file to view its contents:
You can view the contents and any class files and understand how exactly the java program was coded by the developer. This technique can be used to reverse engineer or to decompile any JAR file.
Extracting the Contents of a JAR File
The basic command, using command line, to use for extracting the contents of a JAR file is:
jar xf jar-file [archived-file(s)]
Let’s look at the options and arguments in this command:
- The x option indicates that you want to extract files from the JAR archive.
- The f options indicates that the JAR file from which files are to be extracted is specified on the command line, rather than through stdin.
- The jar-file argument is the filename (or path and filename) of the JAR file from which to extract files.
- archived-file(s) is an optional argument consisting of a space-separated list of the files to be extracted from the archive. If this argument is not present, the Jar tool will extract all the files in the archive.
As usual, the order in which the x and f options appear in the command doesn’t matter, but there must not be a space between them.
When extracting files, the Jar tool makes copies of the desired files and writes them to the current directory, reproducing the directory structure that the files have in the archive. The original JAR file remains unchanged.
CAUTION: When it extracts files, the Jar tool will overwrite any existing files having the same pathname as the extracted files.
Let’s extract some files from the TicTacToe JAR file we’ve been using in previous sections. Recall that the contents of TicTacToe.jar are:
META-INF/MANIFEST.MF TicTacToe.class TicTacToe.class TicTacToe.java audio/ audio/beep.au audio/ding.au audio/return.au audio/yahoo1.au audio/yahoo2.au example1.html images/ images/cross.gif images/not.gif
Suppose you want to extract the TicTacToe class file and the cross.gif image file. To do so, you can use this command:
jar xf TicTacToe.jar TicTacToe.class images/cross.gif
This command does two things:
- It places a copy of TicTacToe.class in the current directory.
- It creates the directory called images, if it doesn’t already exist, and places a copy of cross.gif within it.
- The original TicTacToe JAR file remains unchanged.
As many files as desired can be extracted from the JAR file in the same way. When the command doesn’t specify which files to extract, the jar tool extracts all files in the archive. For example, you can extract all the files in the TicTacToe archive by using this command:
Download
You will be to reach the JD GUI’s author site and download from Heelpbook.net this program by login into Heelpbook.net. It’s totally free.
[wpfilebase tag=»file» ][wpfilebase tag=»file» ]
How to Extract the Source Code of a Java Application ☕
If you write Java applications, you may already know that, to be able to easily use your application on another computer, you need to compile its source code. With Gradle, one of the commands that do this is gradle assemble . Once compiled, an executable Jar file is created (like APPLICATION.jar ).
If you open this file, you will see that the file is incomprehensible to you, unless you are a robot. 🤖
Unfortunately, if you think your source code is completely opaque, you are mistaken. 😟 Effectively, the Jar file is in reality a simple Zip file with another extension. 🤡
If you rename the file as APPLICATION.zip and extract the archive, you should see a few folders there, which contain a lot of interesting files.
- the framework and its version: Spring-Boot-Version: 2.4.4
- the list of dependencies along with their respective version
- and the structure of your application with its files as *.class format
Ouch!, you weren’t expecting to see these files, right? At least, they’re still unusable, for now.
If you look at the same file with your IDE, some of them, like Intellij IDEA, can even decompile this type of file for you! 😮
If you look at the following images, you will see that there is not much difference between the source code and its decompiled version. All comments have been removed along with some final keywords. Do you see any other differences? 🤨
Even if you don’t have an IDE that can decompile a class file for you, other solutions allow you to achieve this goal. I tried one of them in the second part of this post, but the results seem to vary from project to project.
So, if you really want to protect your intellectual property, I suggest you choose another language and above all, host your application yourself. 🙃
Procedure
Warning: decompiling, or reverse engineering, a part of an application seems to fall into gray areas, depending of the jurisdiction.
I suggest you to do this only it if it is your application or if you have the author’s permission. Otherwise, you should only do this if it is necessary to ensure software interoperability.
You can check out the additional links at the end of the post for more information on the topic.
Install JD-core-java
# Clone the repository git clone https://github.com/orangecms/jd-core-java.git # Build the application cd jd-core-java && ./gradlew assemble && cd .. # Move the JAR file to the current directory mv jd-core-java/build/libs/jd-core-java-1.2.jar jd-core-java-1.2.jar # Delete the directory containing the application source code rm -rf jd-core-java
Decompile the JAR files
# Copy the JAR files to decompile to that directory cp ../johnDoeProject/*.jar . # Iterate over all JAR files for JAR in *.jar; do # Get the filename FOLDER="$" # Decompile the JAR file into its proper directory java -jar jd-core-java-1.2.jar $JAR $FOLDER done # Remove decompiled JAR files rm -rf *.jar
Additional links
How to read a class file from an extracted jar file?
My extracted .class file that contains binary & compiled bytecode:
I extracted a .jar file but I can’t read the .class files with Notepad++. I only want to display and study the code.
.class files contain binary, compiled bytecode. Not source code. .java files contain the source code.
2 Answers 2
Use a decompiler. I prefer using Fernflower, or if you use IntelliJ IDEA, simply open .class files from there, because it has Fernflower pre-installed.
Or, go to javadecompilers.com, upload your .jar file, use CFR and download the decompiled .zip file.
However, in some cases, decompiling code is quite illegal, so, prefer to learn instead of decompiling.
And in other cases, code is also obfuscated, so you can’t see method names, because they’re like a(), aB() etc.
Mark the question as answered by ticking my answer and, if you want, gimme a +1. It would be very appreciated. Glad it worked!
Extract the Contents of .zip / .jar files programmatically
Suppose .jar file is the .jar / .zip file to be extracted. destDir is the path where it will be extracted:
java.util.jar.JarFile jar = new java.util.jar.JarFile(jarFile); java.util.Enumeration enum = jar.entries(); while (enum.hasMoreElements()) < java.util.jar.JarEntry file = (java.util.jar.JarEntry) enum.nextElement(); java.io.File f = new java.io.File(destDir + java.io.File.separator + file.getName()); if (file.isDirectory()) < // if its a directory, create it f.mkdir(); continue; >java.io.InputStream is = jar.getInputStream(file); // get the input stream java.io.FileOutputStream fos = new java.io.FileOutputStream(f); while (is.available() > 0) < // write contents of 'is' to 'fos' fos.write(is.read()); >fos.close(); is.close(); >