Using JAR Files: The Basics |
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:Let's look at the options and arguments used in this command:jar cmf manifest jar-file input-file(s)
- 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 is the name (or path and name) of the existing manifest 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 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: trueTo 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
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:myCompany/firstPackage/ myCompany/secondPackage/ myCompany/thirdPackage/ myCompany/fourthPackage/Note that the package names end with a "/".Name: myCompany/firstPackage/ Sealed: true Name: myCompany/thirdPackage/ Sealed: trueLet's suppose further that:
- you named your partial manifest myManifest
- 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 myManifest myJar.jar myCompanyThe 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.
Using JAR Files: The Basics |