Executable Jar Files

JAR (Java Archive) is a platform-independent file format that aggregates many files into one. Multiple Java applets and their requisite components (.class files, images and sounds) can be bundled in a JAR file and subsequently downloaded to a browser in a single HTTP transaction, greatly improving the download speed. The JAR format also supports compression, which reduces the file size, further improving the download time.

Typical usage to combine files into a jar file is:

C:\Java> jar cf myFile.jar *.class *.java

If you specify an entry point for the jar file, the jar file is now executable:

  1. You can run it from the command line: java -jar Drag.jar
  2. You can double-click it on the desktop.
  3. You can run it as an applet (if it extends JApplet)

Manifest Files

A manifest file entry named META-INF/MANIFEST.MF is automatically generated by the jar tool and is always the first entry in the jar file. The manifest file is the place where any meta-information about the archive is stored as name : value pairs.

One possible entry in the manifest file is the entry point, idenfied by the meta name Main-Class.

Specifying an Entry Point

The -e option sets the entry point for stand-alone applications bundled into a jar file.

For example, if DragDemo.class contains the main entry point, then the following command creates an executable jar file:

jar cef DragDemo Drag.jar *.java *.class

Note the order of the arguments. If the entrypoint and archive filename options were reversed (i.e. cfe), then the command would be

jar cfe Drag.jar DragDemo *.java *.class

The resulting manifest file is in a text format inspired by RFC822 ASCII format, so it is easy to view in any text editor.

Specifying a Manifest File

To specify a manifest, you must first prepare a text file containing the information you wish placed in the manifest.

For example, you could create a file called manifest.txt with the following contents:

Main-Class: DragDemo

Warning : The text file from which you are creating the manifest 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.

Next (and finally) you must specify that file when creating the jar file, as in the following example

jar cmf manifest.txt Drag.jar *.java *.class
As before, the order of the arguments must be the same as the order of the options.

If you only need an entrypoint, it is easier to use the entrypoint (-e) option.

The APPLET tag

Changing the APPLET tag in your HTML page to accomodate a JAR file is simple. The JAR file on the server is identified by the ARCHIVE parameter, where the directory location of the jar file should be relative to the location of the html page:

    <applet code=Animator.class 
      width=460 height=160>
      <param name=foo value="bar">

Note that the familiar CODE=myApplet.class parameter must still be present. The CODE parameter, as always, identifies the name of the applet where execution begins. However, the class file for the applet and all of its helper classes are loaded from the JAR file.

The ARCHIVE attribute describes one or more JAR files containing classes and other resources that will be "preloaded". The classes are loaded using an instance of an AppletClassLoader with the given CODEBASE. It takes the form archive = archiveList. The archives in archiveList are separated by ",".

Once the archive file is identified, it is downloaded and separated into its components. During the execution of the applet, when a new class, image or audio clip is requested by the applet, it is searched for first in the archives associated with the applet. If the file is not found amongst the archives that were downloaded, it is searched for on the applet's server, relative to the CODEBASE (that is, it is searched for as in JDK1.0.2).

The archive tag may specify multiple JAR files. Each JAR file must be separated by "," (comma). Each file is downloaded in turn:

    <applet code=Animator.class 
      archive="classes.jar ,  images.jar ,  sounds.jar"
      width=460 height=160>
      <param name=foo value="bar">

There can be any amount of white space between entries within the archive parameter. In addition, the archive tag itself is case-insensitive; it can be lower-case, upper-case, or any combination of lower- and upper-case letters, such as ArCHiVe.

Platform Accomodations

On Microsoft Windows systems, the Java 2 Runtime Environment's installation program will register a default association for JAR files so that double-clicking a JAR file on the desktop will automatically run it with javaw -jar. Dependent extensions bundled with the application will also be loaded automatically. This feature makes the end-user runtime environment easier to use on Microsoft Windows systems.

The Solaris 2.6 kernel has already been extended to recognize the special "magic" number that identifies a JAR file, and to invoke java -jar on such a JAR file as if it were a native Solaris executable. A application packaged in a JAR file can thus be executed directly from the command line or by clicking an icon on the CDE desktop.

You can run a Java program in an executable jar file from the command line by using a -jar option. For example,

java -jar Drag.jar


Jar Archive Technical Note
Jar Archive Tool Reference (Windows version)
Tutorial Introduction to JAR files

Maintained by John Loomis, updated Tue Mar 25 2008