Java Reference
In-Depth Information
When you run a JAR file with the -jar option using the java command, any CLASSPATH setting outside the
manifest file of the JAR file ( test.jar file in the above case) is ignored. Another use of the Class-Path attribute is to
generate an index of all packages using the option i of the jar tool. The following command will generate an index for
all packages in all JAR files listed in the Class-Path attribute of the manifest file in the test.jar file:
jar i test.jar
Sealing a Package in a JAR File
You can seal a package in a JAR file. Sealing a package in a JAR file means that all classes declared in that package
must be archived in the same JAR file. Typically, you seal a package to easily maintain versions of the package. If you
change anything in the package, you just recreate a JAR file. To seal a package in a JAR file, you need to include two
attributes: Name and Sealed . The value for the Name attribute is the name of the package and the Sealed attribute has
value as true. The following entries in a manifest file will seal a package named com.jdojo.archives . Note that the
package name must end with a forward slash (/).
Name: com/jdojo/archives/
Sealed: true
By default, all packages in a JAR file are not sealed. If you want to seal the JAR file itself, you can include a Sealed
attributed, as shown:
Sealed: true
Sealing the JAR file will seal all packages in that JAR file. However, you can override it by not sealing a package
individually. The following entries in a manifest file will seal all packages in the JAR file, except the book/chapter8/
package:
Sealed: true
Name: book/chapter8/
Sealed: false
Using the JAR API
Using JAR API is very similar to using the ZIP API, except that the JAR API includes classes for working with a manifest
file. An object of the Manifest class represents a manifest file. You create a Manifest object in your code as follows:
Manifest manifest = new Manifest();
There are two things you can do with a manifest file: read entries from it and write entries to it. There are separate
ways to deal with entries in the main and individual sections. To add an entry into a main section, get an instance of
the Attributes class using the getMainAttributes() method of the Manifest class and keep adding a name-value
pair to it using its put() method. The following snippet of code adds some attributes to the main section of a manifest
object. The known attribute names are defined as constants in the Attributes.Name class. For example, the constant
Attributes.Name.MANIFEST_VERSION represents the Manifest-Version attribute name.
// Create a Manifest object
Manifest manifest = new Manifest();
 
Search WWH ::




Custom Search