JAR Files

A JAR file is similar to a tar file in that it constitutes an archive of Java class files. JAR files are created with the jar command and the jar command takes many of the same arguments as tar. The jar command automatically performs compression on your class files using the ZIP file format.

You can use the jar command to package all the class files for your application into a single file. If you also tell the jar command the main class to access within the jar file, then you can treat the jar file as a pseudo-object file.

The following table, which I copied from Sun's tutorial website on the jar command, shows the most common uses of the jar command:

OperationCommand
To create a JAR file jar cf jar-file input-file(s)
To view the contents of a JAR file jar tf jar-file
To extract the contents of a JAR file jar xf jar-file
To run an application packaged as a JAR file (requires the Main-class manifest header)
java -jar app.jar

Specifying an Application's Entry Point

You can tell java how to find the entry point within your jar file by creating a manifest file. A manifest file can have any name you choose although manifest.txt is a commonly chosen one. A manifest file should contain the following single statement:

Main-Class: classname

Main-Class is a required keyword and classname is the name of the class that contains your application's main method.

Here is an example manifest file for an application whose main class is formula.lexdriver:

Main-Class: formula.lexdriver

Warnings about Using Jar and Manifest Files

  1. The manifest file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.

  2. Normally you will jar up a directory tree and the directory tree will contain all the files needed for your application. However, should you wish to include files from other directories, and you do not want these files to appear in the jar file prefixed with their directory name, then you can use the -C flag to change to their directory and then grab the files. For example, suppose that you are jarring up the foo.class, and that foo creates an instance of Hello, which can be found in /Users/bvz/cs102/play/Hello.class. If you try to type:
    jar cmf manifest.txt foo.jar foo.class /Users/bvz/cs102/play/Hello.class
    
    then your jar file won't work properly, because Hello.class will get included in your jar file as a subdirectory of /Users/bvz/cs102/play. Instead you should type:
    jar cmf manifest.txt foo.jar foo.class -C /Users/bvz/cs102/play Hello.class
    
    The -C flag causes the jar command to "cd" to the /Users/bvz/cs102/play directory and then grab Hello.class. The jar file will now contain foo.class and Hello.class, which is what you want.

  3. If you use the -jar option, the java interpreter expects all user classes to be bundled in either the jar file or in jar files that are referenced by the manifest file's Class-Path parameter. (The specific statement can be found in the documentation for java's -jar flag and reads "When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.") The Class-Path parameter may only contain references to jar files and the jar files must be in directories local to the machine on which the jar file is being run, not in the jar file itself. The Class-Path parameter may not specify directories in either the jar file itself, or on the local machine that contain class files. If you want classes in your jar file to access external class files that simply exist in a directory, such as for example, java_cup/runtime classes, then you are out of luck and cannot use the -jar option with java.

Creating a Jar File that Contains a Manifest

When you create your jar file, you will must include m as a command line option. This option causes java to use the file specified after the m flag as the manifest file. For example, if the above file is named manifest.txt and your files are in a directory named formula, you could create your jar file with the following command:

jar cmf manifest.txt formula.jar formula

Note that you can specify all of your flags before specifying the files. The files should be specified in the same order as the flags. The name of the jar file does not have to be the same as your main class.

Executing a Jar File with an Application Entry Point

In order to execute a jar file that has an entry point specified, invoke java with the -jar option and the jar file's name. For example:

java -jar formula.jar


Where to Find More Information about Jar Files

You can find more information about jar files and manifest files here.