Table of Contents

Java - (Jar|Java ARchive) File

About

JAR stands for Java ARchive and is file format born in 1996 as a simple extension of the popular ZIP archive format with class and other required resource files:

The primary motivation for its development was so that Java applets and their requisite components (files, images and sounds) can be downloaded to a browser in a single HTTP transaction, rather than opening a new connection for each piece.

With the advent of the Main-Class manifest header, entire applications can be delivered.

Hell

Jar hell is a term similar to describe all the various ways in which the class loading process can end up not working.

To avoid a Jar hell, the shading technique can be used.

Management

Name

Jar Archive name follows the coordinate schema.

Create

You can create JAR files, either:

With the jar command-line utility.

jar cf myPackage.jar *.class

where:

Create an Executable Jar

Creating a executable jar file needs more steps:

echo Main-Class: com.gerardnico.HelloWorld>myManifest
md build\jar
jar cfm build\jar\HelloWorld.jar myManifest -C build\classes

Execute

java -jar myJAR.jar
java -jar myJAR.jar com.example.main arg1 arg2

Verify / Sign

Verification

jar tf build/libs/building-java-libraries.jar
META-INF/
META-INF/MANIFEST.MF
Library.class

You should see the required manifest file —MANIFEST.MF— and the compiled class.

Search class

Windows

for /R %G in (*.jar) do @jar -tvf "%G" | find "ClassName" > NUL && echo %G

where:

Linux

find path/to/jars -name "*.jar" -exec grep -Hsli ClassName  {} \;
for i in $(locate "*.jar");
  do echo $i; jar -tvf $i | grep -Hsi ClassName;
done