Previous | Next | Trail Map | The Java Archive (JAR) File Format | Using JAR Files: The Basics

Modifying a Manifest File

There are a couple of ways in which you can modify the contents of a JAR file. One method, available in both versions 1.1 and 1.2 of the Jar tool, uses the m command-line option to add custom information to the manifest during creation of a JAR file. The m option is described in this section.

Version 1.2 of the Jar tool also provides a u option which you can used to update the contents of an existing JAR file, including its manifest. The u option is covered in the next section.

Probably the most typical reason you would want to customize the manifest of a JAR file is to add a special-purpose header that allows your JAR file to perform a particular desired function. You can see examples of some special-purpose headers in the Understanding the Manifest section of this lesson.

The Jar Tool Command

The basic command has this format:
jar cmf manifest jar-file input-file(s)
Let's look at the options and arguments used in this command:

The c, m, and f options can appear in any order, but there must not be any whitespace between them.

An Example

In version 1.2 of the JavaTM platform, packages within JAR files can be optionally sealed, which means that all classes defined in that package must be archived in the same JAR file. You might want to seal a package, for example, to ensure version consistency among the classes in your software.

A package can be sealed by adding the Sealed header beneath the header naming the package that's to be sealed.:

Name: myCompany/myPackage/
Sealed: true

To insert the Sealed header in a JAR file's manifest, you first need to write a manifest file with the appropriate headers. Actually, the file you write doesn't have to be a complete manifest file; it can contain just enough information for the Jar tool to know where and what information to merge into the default manifest file.

Let's suppose, for example, that your JAR file is to contain these four packages

myCompany/firstPackage/
myCompany/secondPackage/
myCompany/thirdPackage/
myCompany/fourthPackage/
and that you want to seal firstPackage and thirdPackage. To do so, you would write a partial manifest file with contents that look like this:
Name: myCompany/firstPackage/
Sealed: true

Name: myCompany/thirdPackage/
Sealed: true
Note that the package names end with a "/".

Let's suppose further that:

You would create the JAR file with this command:

jar cmf myManifest myJar.jar myCompany

The precise look of the resulting manifest file in myJar.jar would depend upon whether you were using version 1.1 or version 1.2 of the JavaTM Development Kit. In either case, the sealing header would be included for firstPackage and thirdPackage.


Previous | Next | Trail Map | The Java Archive (JAR) File Format | Using JAR Files: The Basics