Java Reference
In-Depth Information
You can think of a JAR file as a special kind of a ZIP file. A JAR file provides many features that are not available in
a ZIP file. You can digitally sign the contents of a JAR file to provide security. It provides a platform-independent file
format. You can use the JAR API to manipulate a JAR file in a Java program.
A JAR file can have an optional META-INF directory to contain files and directories containing information about
application configuration. Table
9-2
lists the entries in a META-INF directory.
Table 9-2.
Contents of META-INF Directory of a JAR File
Name
Type
Purpose
MANIFEST.MF
File
It contains extension and package related data.
INDEX.LIST
File
It contains location information of packages. Class loaders use it to speed up
the class searching and loading process.
X.SF
File
X is the base file name. It stores the signature for the jar file.
X.DSA
File
X is the base file name. It stores the digital signature of the corresponding
signature file.
/services
Directory
This directory contains all service provider configuration files.
The JDK ships a
jar
tool to create and manipulate JAR files. You can also create and manipulate a JAR file using
the Java API using classes in the
java.util.jar
package. Most of the classes in this package are similar to the classes
in the
java.util.zip
package. In fact, most of the classes in this package are inherited from the classes that deal
with the ZIP file format. For example, the
JarEntry
class inherits from the
ZipEntry
class; the
JarInputStream
class
inherits from the
ZipInputStream
class; the
JarOutputStream
class inherits from the
ZipOutputStream
class, etc. The
JAR API has some new classes to deal with a manifest file. The
Manifest
class represents a manifest file. I will discuss
how to use the JAR API later in this chapter. I will discuss the
jar
tool in this section.
To create a JAR file using the
jar
tool, many command-line options are available. There are four basic operations
that you perform using the
jar
tool.
•
Create a JAR file.
•
Update a JAR file.
•
Extract entries from a JAR file.
•
Table
9-3
lists the command-line options for the
jar
tool.
List the contents of a JAR file.
Table 9-3.
Command-line Options for the jar Tool
Option
Description
-c
Create a new JAR file.
-u
Update an existing JAR file.
-x
Extract a named file or all files from a JAR file.
-t
List the table of contents of a JAR file.
-f
Specify the JAR file name.
-m
Include the manifest information from the specified file.
(
continued
)