Java: Working with Jar File

By Xah Lee. Date: . Last updated: .

A jar file (aka Java Archive) is a zip compressed bundle of compiled java class files.

Use jar tool to create or unpack jar files.

View Jar File

# list contents of the file x.jar
jar tf x.jar

How to View Content of Manifest File?

# list MANIFEST content of a jar file
unzip -p filename.jar META-INF/MANIFEST.MF

The file “META-INF/MANIFEST.MF” is basically a index of the archive.

Example of a jar manifest file content:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: hudson
Build-Jdk: 1.6.0_20
Main-Class: clojure.main

Creating Jar File

Here's a example of creating a jar file:

# create a java jar file
# name it aa.jar
# specify the program entrypoint is class Bb
# contains all files of the pattern *.class and cc*/*.class
jar cvfe aa.jar Bb *.class cc*/*.class

Extract Jar File

# extract jar file
jar xvf x.jar