Java Reference
In-Depth Information
Solution
Use
jar
.
Discussion
The
jar
archiver is Java's standard tool for building archives. Archives serve the same pur-
pose as the program libraries that some other programming languages use. Java normally
loads its standard classes from archives, a fact you can verify by running a simple Hello
World program with the
-verbose
option:
java -verbose HelloWorld
Creating an archive is a simple process. The
jar
tool takes several command-line arguments:
the most common are
c
for create,
t
for table of contents, and
x
for extract. The archive
name is specified with
-f
and a filename. The options are followed by the files and director-
ies to be archived. For example:
jar cvf /tmp/MyClasses.jar .
The dot at the end is important; it means “the current directory.” This command creates an
archive of all files in the current directory and its subdirectories into the file
/tmp/
MyClasses.jar
.
Most applications of JAR files depend on an extra file that is always present in a true JAR
file, called a
manifest
. This file always lists the contents of the JAR and their attributes; you
can add extra information into it. The attributes are in the form
name
:
value
, as used in email
headers, properties files (see
Storing Strings in Properties and Preferences
)
, and elsewhere.
Some attributes are required by the application, whereas others are optional. For example,
Running a Program from a JAR
discusses running a main program directly from a JAR; this
requires a
Main-Program
header. You can even invent your own attributes, such as:
MySillyAttribute: true
MySillynessLevel: high (5'11")
You store this in a file called, say,
manifest.stub
,
[
60
]
and pass it to
jar
with the
-m
switch.
jar
includes your attributes in the manifest file it creates: