The jar utility provided with the JDK is a simple tool for creating and reading JAR files. Its user interface isn’t particularly friendly. It mimics the Unix tar (tape archive) command. If you’re familiar with tar, you’ll recognize the following incantations:
jar -cvf jarFile path [ path ] [ ... ]
Create jarFile containing path(s).
jar -tvf jarFile [ path ] [ ... ]
List the contents of jarFile, optionally showing just path(s).
jar -xvf jarFile [ path ] [ ... ]
Extract the contents of jarFile, optionally extracting just path(s).
In these commands, the flag letters c, t, and x tell jar whether it is creating an archive,
listing an archive’s contents, or extracting files from an archive. The f means that the
next argument is the name of the JAR file on which to operate. The optional v flag tells
jar to be verbose when displaying information about files. In verbose mode, you get
information about file sizes, modification times, and compression ratios.
Subsequent items on the command line (i.e., anything aside from the letters telling jar
what to do and the file on which jar should operate) are taken as names of archive items.
If you’re creating an archive, the files and directories you list are placed in it. If you’re
extracting, only the filenames you list are extracted from the archive. (If you don’t list
any files, jar extracts everything in the archive.)
For example, let’s say we have just completed our new game, spaceblaster. All the files
associated with the game are in three directories. The Java classes themselves are in the
spaceblaster/game directory, spaceblaster/images contains the game’s images, and spaceblaster/
docs contains associated game data. We can pack all this in an archive with this
command:
% jar -cvf spaceblaster.jar spaceblaster
Because we requested verbose output, jar tells us what it is doing:
adding:spaceblaster/ (in=0) (out=0) (stored 0%)
adding:spaceblaster/game/ (in=0) (out=0) (stored 0%)
adding:spaceblaster/game/Game.class (in=8035) (out=3936) (deflated 51%)
adding:spaceblaster/game/Planetoid.class (in=6254) (out=3288) (deflated 47%)
adding:spaceblaster/game/SpaceShip.class (in=2295) (out=1280) (deflated 44%)
adding:spaceblaster/images/ (in=0) (out=0) (stored 0%)
adding:spaceblaster/images/spaceship.gif (in=6174) (out=5936) (deflated 3%)
adding:spaceblaster/images/planetoid.gif (in=23444) (out=23454) (deflated 0%)
adding:spaceblaster/docs/ (in=0) (out=0) (stored 0%)
adding:spaceblaster/docs/help1.html (in=3592) (out=1545) (deflated 56%)
adding:spaceblaster/docs/help2.html (in=3148) (out=1535) (deflated 51%)
jar creates the file spaceblaster.jar and adds the directory spaceblaster, adding the directories
and files within spaceblaster to the archive. In verbose mode, jar reports the
savings gained by compressing the files in the archive.
We can unpack the archive with this command:
% jar -xvf spaceblaster.jar
Likewise, we can extract an individual file or directory with:
% jar -xvf spaceblaster.jar filename
But, of course, you normally don’t have to unpack a JAR file to use its contents; Java
tools know how to extract files from archives automatically. We can list the contents of
our JAR with the command:
% jar -tvf spaceblaster.jar
Here’s the output; it lists all the files, their sizes, and their creation times:
0 Thu May 15 12:18:54 PDT 2003 META-INF/
1074 Thu May 15 12:18:54 PDT 2003 META-INF/MANIFEST.MF
0 Thu May 15 12:09:24 PDT 2003 spaceblaster/
0 Thu May 15 11:59:32 PDT 2003 spaceblaster/game/
8035 Thu May 15 12:14:08 PDT 2003 spaceblaster/game/Game.class
6254 Thu May 15 12:15:18 PDT 2003 spaceblaster/game/Planetoid.class
2295 Thu May 15 12:15:26 PDT 2003 spaceblaster/game/SpaceShip.class
0 Thu May 15 12:17:00 PDT 2003 spaceblaster/images/
6174 Thu May 15 12:16:54 PDT 2003 spaceblaster/images/spaceship.gif
23444 Thu May 15 12:16:58 PDT 2003 spaceblaster/images/planetoid.gif
0 Thu May 15 12:10:02 PDT 2003 spaceblaster/docs/
3592 Thu May 15 12:10:16 PDT 2003 spaceblaster/docs/help1.html
3148 Thu May 15 12:10:02 PDT 2003 spaceblaster/docs/help2.html
0 comments:
Post a Comment