|
Book Excerpt Index
You can modify the contents of a JAR file in a couple of ways.
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
section called Updating a JAR File.
The Jar tool automatically puts a default manifest with pathname
META-INF/MANIFEST.MF into any JAR file you create. You can
enable special JAR file functionality, such as package sealing, by
modifying the default manifest.
Typically, this involves adding special-purpose headers to the
manifest that allow the JAR file to perform a particular desired
function. For
examples of some special-purpose headers, and for more information
about manifest files in general, see the
Understanding the Manifest
section of this lesson.
The Jar tool's m option allows you to add information to
the default manifest during creation of a JAR file. You must first
prepare a text file containing the information you wish to add to the
default manifest. You can then use the Jar tool's m option to
add the information in your file to the default manifest.
The basic command has this format:
jar cmf manifest-addition jar-file input-file(s)
Let's look at the options and arguments used in this command:
- The
c option indicates that you want to create
a JAR file.
- The
m option indicates that you want to merge
information from an existing manifest file into the manifest
file of the JAR file you're creating.
- The
f option indicates that you want the output to go to
a file (the JAR file you're creating) rather than to stdout.
manifest-addition is the name (or path and name) of the existing
text file whose contents you want included in the JAR file's
manifest.
jar-file is the name that you want the resulting JAR
file to have.
- The
input-file(s) argument is a space-delimited list
of one or more files that you want to be placed in your JAR file.
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 Java 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
The default manifest created by the Jar tool does not contain any
Sealed headers, of course, because packages are not sealed
by default. To seal a package, you therefore have to add the
Sealed header yourself.
To insert the Sealed header in a JAR file's manifest, you
first need to write a text file containing the
appropriate headers. 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.
Let's suppose, for example, that your JAR file is to contain these
four packages:
myCompany/firstPackage/
myCompany/secondPackage/
myCompany/thirdPackage/
myCompany/fourthPackage/
To seal firstPackage and thirdPackage, you would write
a text 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 "/". This file
contains the information that needs to be added to the default
manifest to seal the desired packages. Each Sealed header is
immediately preceeded by a Name header that indicates
which package is to be sealed.
Let's suppose that:
- you named your text file
sealInfo
- the JAR file you want to create will have the name
myJar.jar
- the current directory is the parent directory of
myCompany
You would create the JAR file with this command:
jar cmf sealInfo 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 Java Development Kit. In
either case, the Sealed header would be included for
firstPackage and thirdPackage. If you were using
the version 1.2 Jar tool, the manifest would look like this:
Manifest-Version: 1.0
Name: myCompany/firstPackage/
Sealed: true
Name: myCompany/thirdPackage/
Sealed: true
|
Only the first line, Manifest-Version: 1.0, is part of
the default manifest. The other lines are in the manifest because
you added them when the Jar file was created by using the m
option.
|