Java Beans   «Prev  Next»
Lesson 4Working with JAR files
ObjectiveUse the JAR utility to manipulate JAR files.

Working with JAR Files

This lesson discusses how to use the JAR utility to manipulate JAR files. Like all other JDK tools, the JAR utility is a command-line tool. Its syntax follows:

jar

Jar Argument Options
The Files argument specifies the files to be used when working with a JAR file and varies according to the Options.

Inside the JAR file

To find out what files are contained in a JAR file, you simply use the f and t options with the JAR utility, like this:

jar ft SomeArchive.jar

The f option is necessary to inform the JAR utility which JAR file's contents you would like to list.
The results of this command will include all the files contained within the SomeArchive.jar JAR file.

BeansBook.jar example:

We create an archive named BeansBook.jar that contains the GenericButtonAdapter class from the BeansBook.util package. We go to the directory that contains the BeansBook package directory, and execute the following command:
jar cvf BeansBook.jar BeansBook/util/GenericButtonAdapter.class

This command says that we are creating a new archive named BeansBook.jar, which contains the BeansBook/util/GenericButtonAdapter.class file, and that the jar program should produce some verbose output when it creates the archive. The resulting output looks like this:
adding: BeansBook/util/GenericButtonAdapter.class in=2999 out=1507
deflated 49.0%

This tells us that the BeansBook/util/GenericButtonAdapter.class file was added to the archive and that its size was deflated by 49% due to compression.
Now let us examine the contents of the archive by executing the following command:
jar tvf BeansBook.jar

This command says that we want to list the table of contents of the archive file named BeansBook.jar. The program then produces the following output:
179 Sat Feb 15 16:27:04 EST 1997 META-INF/MANIFEST.MF
2999 Sat Feb 08 14:33:18 EST 1997
BeansBook/util/GenericButtonAdapter.class

The resulting verbose output shows that the archive contains a file named manifest.mf in directory META-INF. This is the manifest file for the archive. The JAR file also contains the file BeansBook/util/GenericButtonAdapter.class. This listing also includes the original size of each element along with its time stamp.

To get a better idea of what these entries look like, let us extract the manifest from the BeansBook.jar archive we just created, using the following command:
jar xf BeansBook.jar META-INF

This will extract everything in the archive from the directory META-INF, which includes the manifest file manifest.mf. If the META-INF directory does not already exist, it will be created when the manifest is extracted. The contents of the manifest look like this:
Manifest-Version: 1.0
Name: BeansBook/util/GenericButtonAdapter.class
Digest-Algorithms: MD5 SHA
MD5-Digest: wuX4KYNI+D3QYBTtNn6wdA==
SHA-Digest: R8cIwi1GSAgAdwAdrxb9AXlSBV8=

Jar Utility - Exercise

Click the Exercise link below to use the JAR utility to examine the buttons.jar JAR file that ships with the BDK.
Jar Utility - Exercise
In the next lesson, the JAR utility will be used to package a Bean.