Skip to content →

Opening a jar

The idea

I was working on a bash script that copied a bunch of jar files from S3. As a person who has avoided Java so far in my career, I don’t know much about jar files, JVM, etc. But I was curious what was inside these jar files, so I tried to open it.

The file is called nullify.jar. It was in a directory with a bunch of other jar files in a lib folder, so I assume that these are libraries (collections of functions and classes that developers can leverage to create their own programs).

At this point I am expecting nullify.jar to contain some Java classes and functions. But how do I open it so I can see the Java code?

I inspected the file using the unix file command:

$ file ~/Downloads/nullify.jar
Java archive data (JAR)

So the “ar” in “jar” is short for archive.

Now I turn to the Internet for help, and find an article that explains how to open a .jar.

Actually, you can open a .jar or you can execute it. But in order to execute it you need to have a Java Runtime Environment (JRE) installed on your computer. Do I have one?

$ jre
zsh: command not found: jre

I found some oracle documentation that explains how to install a JRE. It turns out I already had it installed when I installed Java.

$ java -version
java version "14.0.2" 2020-07-14
Java(TM) SE Runtime Environment (build 14.0.2+12-46)
Java HotSpot(TM) 64-Bit Server VM (build 14.0.2+12-46, mixed mode, sharing)

It looks like I have Java 14.0.2 installed on my machine. I have a JRE and something called Java HotSpot which I don’t know or really care to know much about at this point.

Now that I have a JRE installed I am going to try to run nullify.jar.

$ java ~/Downloads/nullify.jar
Error: Could not find or load main class .Users.me.Downloads.nullify.jar
Caused by: java.lang.ClassNotFoundException: /Users/me/Downloads/nullify/jar

I kind of expected that because I think nullify.jar is a library, not a program that should be executed. Instead of executing the jar, I will try to view its contents. I want to see the Java source code.

I found some documentation that explains how to unpack a .jar file using the jar command.

$ cd ~/Downloads
$ jar xf nullify.jar
$ ls
nullify.jar com META-INF

It created two directories: com and META-INF and a file called MANIFEST.MF inside of it.

$ cat META-INF/MANIFEST.MF
Manifest-Version: 1.0

$ tree com
com
└── example
    └── data
        └── hive
            └── udf
                └── NullTreatment.class

4 directories, 1 file

Excellent. Now I’ll check out the source code of NullTreatment.class

$ cat com/example/data/hive/udf/NullTreatment.class
����3)
�*+��+�M,��,��
              , ���+�

&(
   *
*"#
   $%&
�'(%

It looks like compiled machine code. I found this site that decompiles compiled Java code and was able to see the actual source.

// 
// Decompiled by Procyon v0.5.36
// 

package com.example.data.hive.udf;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.hive.ql.exec.UDF;

public class NullTreatment extends UDF
{
    public Text evaluate(final Text input) {
        if (input == null) {
            return null;
        }
        final String val = input.toString();
        if (val.equalsIgnoreCase("") || val.equalsIgnoreCase("null") || val.equalsIgnoreCase("'null'")) {
            return null;
        }
        return input;
    }
}

Conclusion

It took a lot of steps to view the source code. Using the online decompiler seemed sketchy, too. There are probably better ways to do this.

Published in Today I Learned