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 Argument Options
The Files argument specifies the files to be used when working with a JAR file and varies according to the Options.
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
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
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:
Digest-Algorithms: MD5 SHA