Java Reference
In-Depth Information
45 invokevirtual #26 <Method void println(java.lang.Object)>
48 iinc 1 1
51 iload_1
52 aload_0
53 arraylength
54 if_icmplt 5
57 return
A JAR file packages a subdirectory and its descendants into a single file. A Java
CLASSPATH
specification may contain a JAR filename everywhere it might
contain a directory name. Let's say you use the GPL'ed Java personal finance
program called
jgnash
and you've compiled it from source, so you have a direc-
tory off your home directory called
jgnash/bin
. Suppose you run the program
by directly invoking
java
to run the class
jgnashMain
and you have
$HOME/jgnash/bin
on your
CLASSPATH
. You could clean up the mess on your
hard drive by using the
jar
command to squash all the files in
jgnash/bin
together into a single JAR file, as shown in Example 5.20.
Example 5.20
Making a JAR file
$ cd ; mkdir jars
$ jar cvf jars/jgnash.jar jgnash/bin
You could then replace the
$HOME/jgnash/bin
entry in your
CLASSPATH
with
$HOME/jars/jgnash.jar
. After that you would still run
jgnash
with
exactly the same
java
command you always did, but now you got rid of the
cluttered pile of files.
This is only the most basic purpose of
jar
, however. Its uses extend well
beyond merely concatenating and compressing collections of
.class
files.
5.11.1
One of the best uses of
jar
is to package applications for distribution. You can
put a large Java application into a single file with
jar
, and by using a
manifest
(which we are about to discuss) you can nominate the main class to run in that
JAR file. You can then provide a shell script (and a batch file, if you are also
deploying to Microsoft Windows) that will set the
CLASSPATH
to point to the
Deploying Applications