Java – Decompile JAR files and view Source Code


To totally unlock this section you need to Log-in


Login

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:

  • Download Windows Version.
  • Download Linux Version.
  • Download Mac Version.
  • Now, Extract the ZIP file and you will get these files (we are showing only Windows version), then double click on JD-GUI icon:

Java - Decompile JAR files and view Source Code

And You get the home screen like the following:

Java - Decompile JAR files and view Source Code

Click on File - > Open and choose any JAR file of your choice:

Java - Decompile JAR files and view Source Code

After opening JAR file, you will get a window like this:

Java - Decompile JAR files and view Source Code

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:

Java - Decompile JAR files and view Source Code

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:

jar xf TicTacToe.jar

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" id="269"]
[wpfilebase tag="file" id="270"]