Java Reference
In-Depth Information
The generated
INDEX.LIST
file contains location information for all packages in all JAR files listed in the Class-
Path attribute of the
test.jar
file. You can include an attribute called Class-Path in the manifest file of a JAR file. It is a
space-separated list of JAR files. The attribute value is used to search and load classes when you run the JAR file.
Extracting an Entry from a JAR File
You can extract all or some entries from a JAR file using the option
x
with the
jar
command. To extract all entries from
a test.jar file, you use
jar xf test.jar
The option
x
indicates that you want to extract the entries from the JAR file. The option
f
indicates that you are
specifying the file name, which is
test.jar
. The above command will extract all entries from
test.jar
file in the
current working directory. It will create the same directory structure as it exists in the
test.jar
file. For example,
if
book/HelloWorld.class
is an entry in the
test.jar
file, the above command will create a
book
directory in the
current working directory and extract the
HelloWorld.class
file into the
book
directory. Any existing file during the
extraction of an entry is overwritten. The JAR file,
test.jar
in this example, is unchanged by the above command.
To extract individual entries from a JAR file, you need to list them at the end of the command. The entries should
be separated by a space. The following command will extract
A.class
and
book/HelloWorld.class
entries from a
test.jar
file:
jar xf test.jar A.class book/HelloWorld.class
To extract all class files from a book directory, you can use the following command:
jar xf test.jar book/*.class
Listing the Contents of a JAR File
Use the option
t
with the
jar
command to list the table of contents of a JAR file on the standard output.
jar tf test.jar
The Manifest File
A JAR file differs from a ZIP file in that it may optionally contain a manifest file named
MANIFEST.MF
in the
META-INF
directory. The manifest file contains information about the JAR file and its entries. It can contain information about
the CLASSPATH setting for the JAR file. Its main entry class is a class with the
main()
method to start a stand-alone
application, version information about packages, etc.
A manifest file is divided into sections separated by a blank line. Each section contains name-value pairs. A new
line separates each name-value pair. A colon separates a name and its corresponding value. A manifest file must end
with a new line. The following is a sample of the content of a manifest file:
Manifest-Version: 1.0
Created-By: 1.8.0_20-ea-b05 (Oracle Corporation)
Main-Class: com.jdojo.intro.Welcome
Profile: compact1