Previous | Next | Trail Map | The Java Archive (JAR) File Format | Using JAR Files: The Basics

Updating a JAR File

The Jar tool in version 1.2 of the JavaTM Development Kit provides a u option which you can use to update the contents of an existing JAR file by modifying its manifest or by adding files.

The basic command for adding files has this format:

jar uf jar-file input-file(s)

In this command:

Any files already in the archive having the same pathname as a file being added will be overwritten.

As when creating a new JAR file, you can optionally use the -C option to indicate a change of directory.

You can combine the u option with the m option to update an existing JAR file's manifest:

jar umf manifest jar-file

In this command:

Examples

Recall that TicTacToe.jar has these contents:
META-INF/MANIFEST.MF
TicTacToe.class
audio/
audio/beep.au
audio/ding.au
audio/return.au
audio/yahoo1.au
audio/yahoo2.au
images/
images/cross.gif
images/not.gif

Suppose that you want to add the file images/new.gif to the JAR file. You could accomplish that by issuing this command from the parent directory of the images directory:

jar uf TicTacToe.jar images/new.gif

The revised JAR file would have this table of contents:

META-INF/MANIFEST.MF
TicTacToe.class
audio/
audio/beep.au
audio/ding.au
audio/return.au
audio/yahoo1.au
audio/yahoo2.au
images/
images/cross.gif
images/not.gif
images/new.gif

You can use the -C option to "change directories" during execution of the command. For example:

jar uf TicTacToe.jar -C images new.gif
This command would change to the images directory before adding new.gif to the JAR file. The images directory would not be included in the pathname of new.gif when it's added to the archive, resulting in a table of contents that looks like this:
META-INF/MANIFEST.MF
TicTacToe.class
audio/
audio/beep.au
audio/ding.au
audio/return.au
audio/yahoo1.au
audio/yahoo2.au
images/
images/cross.gif
images/not.gif
new.gif

As a final example, suppose you want to modify the default manifest of TicTacToe.jar by adding some version and vendor information. (Version and vendor information is contained in special headers that you can add to a JAR file's manifest. See the section on Understanding the Manifest for information about special headers.) You would first prepare a text file containing the headers that you wish to add to the default manifest. Your text file might consist of this information, for example:

Name: TicTacToe.class
Implementation-Title: "TicTacToe demo" 
Implementation-Version: "build57"
Implementation-Vendor: "Sun Microsystems, Inc."

If the file containing this information was called versionInfo, you would add the information to the manifest in TicTacToe.jar by using this command:

jar umf versionInfo TicTacToe.jar
After running this command, TicTacToe.jar's manifest would contain the information from your versionInfo file.


Previous | Next | Trail Map | The Java Archive (JAR) File Format | Using JAR Files: The Basics