The easiest way to package a Java application is in a jar file. A jar file (Java Archive) is a specially formatted zip file with a .jar extension. What may not be realized is that jar files can be executed directly on most operating systems (so they can act like exe files on Windows). This special behaviour is provided by a file in the jar called the manifest file. This file describes certain aspects of the jar and provides supporting information. For example the manifest file is used to hold the signature if the jar file is signed. The aspect that this article focuses on, however, is the ability of the manifest file to allow a jar file to be run directly and to specify a default class path.

The manifest file is always called MANIFEST.MF and resides in a directory called META-INF in the root of the jar. The manifest file is a simple text file so can be opened and edited with any text editor. NOTE: The manifest file must have a single blank line at the end or under some circumstances it will not be correctly processed. To make a jar file runnable it is necessary to specify the class to start. This is done with the attribute Main-Class. If the application needs resources in other jar files the Class-Path attribute is used to specify them. The Class-Path attribute can only reference files that are outside the current jar (for example jars that are peers of the current jar in the filesystem) it can not reference jar files that are contained within the current jar. An example of such a manifest file is shown below.

Manifest-Version: 1.0
Main-Class: com.example.Example
Class-Path: util.jar

Attemping to run this jar file (with a command such as java -jar example.jar) would run the class com.example.Example and place the jar file util.jar on the classpath. The util.jar file would be in the same directory as example.jar. The application that has the above manifest file would have a directory structure like this:


Jar files can be created with the jar utility that is supplied with the SDK or, more easily, with either and ant script or an IDE. Personally, I create executable jar files using the jar export wizard in Eclipse.

While an executable jar file is a good way to package a very simple application that you are going to be supporting quite closely any application that is for general consumption of the masses needs to have a platform specific installer. There are plenty of such installers around although I no longer know of a free one.