Book Excerpt Index
Creating a JAR FileThe basic format of the command for creating a JAR file is: jar cf jar-file input-file(s)Let's look at the options and arguments used in this command:
The This command will generate a compressed JAR file and place it in the current directory. The command will also generate a default manifest file for the JAR archive.
You can add any of these additional options to the
In version 1.1, the JAR-file format supports only ASCII filenames.
Version 1.2 adds support for UTF8-encoded names.
The
To package this demo into a single JAR file
named jar cvf TicTacToe.jar TicTacToe.class audio imagesThe audio and images arguments represent directories,
so the Jar tool will recursively place them and their contents in
the JAR file.
The generated JAR file TicTacToe.jar will be placed in the
current directory. Because the command used the v
option for verbose output, you'd see something similar to this output
when you run the command:
adding: TicTacToe.class (in=3825) (out=2222) (deflated 41%) adding: audio/ (in=0) (out=0) (stored 0%) adding: audio/beep.au (in=4032) (out=3572) (deflated 11%) adding: audio/ding.au (in=2566) (out=2055) (deflated 19%) adding: audio/return.au (in=6558) (out=4401) (deflated 32%) adding: audio/yahoo1.au (in=7834) (out=6985) (deflated 10%) adding: audio/yahoo2.au (in=7463) (out=4607) (deflated 38%) adding: images/ (in=0) (out=0) (stored 0%) adding: images/cross.gif (in=157) (out=160) (deflated -1%) adding: images/not.gif (in=158) (out=161) (deflated -1%)
You can see from this output that the JAR file jar cvf0 TicTacToe.jar TicTacToe.class audio images You might want to avoid compression, for example, to increase the speed with which a JAR file could be loaded by a browser. Uncompressed JAR files can generally be loaded more quickly than compressed files because the need to decompress the files during loading is eliminated. However, there's a tradeoff in that download time over a network may be longer for larger, uncompressed files.
The Jar tool will accept arguments that use the wildcard jar cvf TicTacToe.jar *
Though the verbose output doesn't indicate it, the Jar tool
automatically adds a manifest file
to the JAR archive with pathname
In the above example, the files in the archive retained their
relative pathnames and directory structure.
The Jar tool in version 1.2 of the JavaTM
Development Kit provides the
As an example, suppose you wanted put audio files and gif images used
by the TicTacToe demo into a JAR file, and that you wanted all the files
to be on the top level, with no directory hierarchy. You could
accomplish that by issuing this command from the parent directory of
the jar cf ImageAudio.jar -C images * -C audio *The -C images part of this command directs the Jar tool to
go to the images directory, and the * following
-C images directs the Jar tool to
archive all the contents of that directory. The -C audio *
part of the command then does the same with the
audio directory. The resulting JAR
file would have this table of contents:
META-INF/MANIFEST.MF cross.gif not.gif beep.au ding.au return.au yahoo1.au yahoo2.auBy contrast, suppose that you used a command that didn't employ the -C option:
jar cf ImageAudio.jar images audioThe resulting JAR file would have this table of contents: META-INF/MANIFEST.MF images/cross.gif images/not.gif audio/beep.au audio/ding.au audio/return.au audio/yahoo1.au audio/yahoo2.au |