Concept of Java Archive (JAR)

Java Archive (JAR) files are Java’s version of ZIP files. In fact, JAR uses the concept of ZIP which is basically an archive file format supporting lossless data compression. There are two main uses of JAR files which I shall explain here. The first use is to compress (make a smaller size) a number of files into one file (archiving). The second use is to make a Java executable JAR file.

 

Compressing files to an executable JAR

The idea is to compress multiple files (mainly the Java class files) into a single JAR archive. JAR files can be opened with WinZip or 7-Zip. In terms of Java applications, the ability to archive any number of source or class files into one single archive represents the biggest advantage – distributing one file containing hundreds of files is so much easier than distributing hundreds of files separately.

The jar utility program from JDK is run from the command line (DOS prompt or LINUX bash for example, depending on our OS). Here is how to create a compressed JAR file:

jar -cvmf manifest_file archive_name.jar files

Let’s look at each part of that command line.

jar :  The command to run the jar utility.

-cvmf Create a new archive with the file name specified. These four options are selected from this list of common options:

– c create new archive
– t list table of contents for archive
– x extract named (or all) files from archive
– u update existing archive
– v generate verbose output on standard output
– f specify archive file name
– m include manifest information from specified manifest file
– 0 store only; use no ZIP compression
– M do not create a manifest file for the entries
– i generate index information for the specified jar files
– C change to the specified directory and include the following file

Multiple options can be used together. They all must appear after the “jar” command with no white space separating them.

manifest_file : Name of the file that includes manifest information

archive_name.jar Name of the JAR file. This can only be included if we use the ‘f’ option.

files : Names of all the files we want to put in the jar file. This could be just one name or a list of multiple names separated by space. Names can use pattern matching characters to match multiple files.

NOTE: To create an executable JAR file we must store compiled class files in the archive, as well as or instead of Java source files, since Java source files cannot be run.

 
The whole process is divided into two steps. They are described below.

 

Step 1. Creating an Executable JAR

All JAR files contain something called a manifest file which holds information Java wants to know. One piece of information a manifest file may contain is the name of a class that will be run if the JAR file is executed.

The first thing we must do is create a text file that lists the “Main” class – the class that has the main() method we want to execute when the JAR is executed. The package related first example placed under C:\test directory does have the Main class containing main() method that I want to execute. There I can create a text file called “manifest.txt” with the following command:

manifest.txt

Main-Class: techguru.example.Main

IMPORTANT: The text file only needs the one line of text for this purpose. However, the file must end with a blank line or this will not work, i.e. the file has two lines in it – the second one is empty. Note too the class is called “techguru.example.Main” and not “Main.java” (the file containing the source code) or “Main.class” (the file containing the byte codes). As our class file is in a package hierarchy, we must use the fully-qualified name of the class.

I then run the jar utility with this command line:

jar -cvmf manifest.txt example.jar techguru\*

With this line, I am telling the jar command to create a JAR file (option c), generating verbose output on standard output (option v) with modifications to the manifest file (option m) as specified within manifest.txt, naming the JAR file (option f) as example.jar and including everything that matches the given pattern for files.

 

Step 2. Running an Executable JAR from Command Line

I can run it from the command line like this:

java -jar example.jar

 

The whole process is shown below: