java
Packaging Programs in JAR Files
The Java™ Archive (JAR) file format enables you to bundle multiple files into a single archive file. Typically a JAR file contains the class files and auxiliary resources associated with applets and applications.
The JAR file format provides many benefits:
- Security: You can digitally sign the contents of a JAR file. Users who recognize your signature can then optionally grant your software security privileges it wouldn't otherwise have.
- Decreased download time: If your applet is bundled in a JAR file, the applet's class files and associated resources can be downloaded to a browser in a single HTTP transaction without the need for opening a new connection for each file.
- Compression: The JAR format allows you to compress your files for efficient storage.
Packaging for extensions:
The extensions framework provides a means by which you can add
functionality to the Java core platform, and the JAR file format defines
the packaging for extensions. Java 3D™ and JavaMail™ are examples of
extensions developed by Sun™. By using the JAR file format, you
can turn your software into extensions as well. - Package Sealing: Packages stored in JAR files can be optionally sealed so that the package can enforce version consistency. Sealing a package within a JAR file means that all classes defined in that package must be found in the same JAR file.
- Package Versioning: A JAR file can hold data about the files it contains, such as vendor and version information.
- Portability: The mechanism for handling JAR files is a standard part of the Java platform's core API.
Using JAR Files
JAR files are packaged with the ZIP file format, so you can use them for tasks such as lossless data compression, archiving, decompression, and archive unpacking. These tasks are among the most common uses of JAR files, and you can realize many JAR file benefits using only these basic features.
Even if you want to take advantage of advanced functionality provided by the JAR file format such as electronic signing, you'll first need to become familiar with the fundamental operations.
To perform basic tasks with JAR files, you use the Java Archive Tool provided as part of the Java Development Kit. Because the Java Archive tool is invoked by using the jar command, this tutorial refers to it as 'the Jar tool'.
As a synopsis and preview of some of the topics to be covered in this section, the following table summarizes common JAR file operations:
|
Common JAR file operations |
|
|
Operation |
Command |
|
To create a JAR file |
jar cf jar-file input-file(s) |
|
To view the contents of a JAR file |
jar tf jar-file |
|
To extract the contents of a JAR file |
jar xf jar-file |
|
To extract specific files from a JAR file |
jar xf jar-file archived-file(s) |
|
To run an application packaged as a JAR file (requires the Main-class manifest header) |
java -jar app.jar |
|
To invoke an applet packaged as a JAR file |
<applet code=AppletClassName.classarchive="JarFileName.jar" width=width height=height> </applet> |
Creating a JAR File
The basic format of the command for creating a JAR file is:
jar cf jar-file input-file(s)(space sepatated)
The options and arguments used in this command are:
- The c option indicates that you want to create a JAR file.
- The f option indicates that you want the output to go to a file rather than to stdout.
- jar-file is the name that you want the resulting JAR file to have. You can use any filename for a JAR file. By convention, JAR filenames are given a .jar extension, though this is not required.
The input-file(s) argument is a space-separated list of
one or more files that you want to include in your JAR file. The input-file(s) argument can contain
the wildcard * symbol. If
any of the "input-files" are directories, the contents of those
directories are added to the JAR archive recursively.
The c and f options can appear in either order, but there must not be any space between them.
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.
Note: The metadata in the JAR file, such as the entry names, comments, and contents of the manifest, must be encoded in UTF8. (UTF-8 stands for Unicode Transformation Format-8. ... UTF-8 encodes each Unicode character as a variable number of 1 to 4 octets)
You can add any of these additional options to the cf options of the basic command:
|
jar command options Option |
Description |
|
v |
Produces verbose output on stdout while the JAR file is being built. The verbose output tells you the name of each file as it's added to the JAR file. |
|
0 (zero) |
Indicates that you don't want the JAR file to be compressed. |
|
M |
Indicates that the default manifest file should not be produced. |
|
m |
Used to include manifest information from an existing manifest file. The format for using this option is: jar cmf existing-manifest jar-file input-file(s) Warning: The manifest must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return. |
|
-C |
To change directories during execution of the command. See below for an example. |
Note: When you create a JAR file, the time of creation is stored in the JAR file. Therefore, even if the contents of the JAR file do not change, when you create a JAR file multiple times, the resulting files are not exactly identical. You should be aware of this when you are using JAR files in a build environment. It is recommended that you use versioning information in the manifest file, rather than creation time, to control versions of a JAR file...
An Example
Let us look at an example. A simple TicTacToe applet. You can see the source code of this Applet at TicTacToe.java. This demo contains a bytecode class file, audio files, and images having this structure:

TicTacToe folder Hierarchy
The audio and images subdirectories contain sound files and GIF images used by the applet.
You can obtain all these files from jar/examples directory when you download the entire Tutorial online. To package this demo into a single JAR file named TicTacToe.jar, you would run this command from inside the TicTacToe directory:
jar cvf TicTacToe.jar TicTacToe.class audio images
The 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 would 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 TicTacToe.jar is compressed. The Jar tool compresses files by default. You can turn off the compression feature by using the 0 (zero) option, so that the command would look like:
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 is 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 * symbol. As long as there weren't any unwanted files in the TicTacToe directory, you could have used this alternative command to construct the JAR file:
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 path name META-INF/MANIFEST.MF. See the Working with Manifest Files: The Basics section for information about manifest files.
In the above example, the files in the archive retained their relative path names and directory structure. The Jar tool provides the -C option that you can use to create a JAR file in which the relative paths of the archived files are not preserved. It's modeled after TAR's -C option.
As an example, suppose you wanted to 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 images and audio directories:
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.au
By contrast, suppose that you used a command that did not employ the -C option:
jar cf ImageAudio.jar images audio
The 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
Default Manifest
When you create a JAR file, it automatically receives a default manifest file. There can be only one manifest file in an archive, and it always has the pathname
META-INF/MANIFEST.MF
When you create a JAR file, the default manifest file simply contains the following:
Manifest-Version: 1.0
Created-By: 1.6.0 (Sun Microsystems Inc.)
These lines show that a manifest's entries take the form of "header: value" pairs. The name of a header is separated from its value by a colon. The default manifest conforms to version 1.0 of the manifest specification and was created by the 1.6.0 version of the JDK.
The manifest can also contain information about the other files that are packaged in the archive. Exactly what file information should be recorded in the manifest depends on how you intend to use the JAR file. The default manifest makes no assumptions about what information it should record about other files.
Extracting the Contents of a JAR File
The basic command to use for extracting the contents of a JAR file is:
jar xf jar-file [archived-file(s)]
Let's look at the options and arguments in this command:
- The x option indicates that you want to extract files from the JAR archive.
- The f options indicates that the JAR file from which files are to be extracted is specified on the command line, rather than through stdin.
- The jar-file argument is the filename (or path and filename) of the JAR file from which to extract files.
- archived-file(s) is an optional argument consisting of a space-separated list of the files to be extracted from the archive. If this argument is not present, the Jar tool will extract all the files in the archive.
As usual, the order in which the x and f options appear in the command doesn't matter, but there must not be a space between them.
When extracting files, the Jar tool makes copies of the desired files and writes them to the current directory, reproducing the directory structure that the files have in the archive. The original JAR file remains unchanged.
An Example
Let's extract some files from the TicTacToe JAR file we've been using in previous sections. Recall that the contents of TicTacToe.jar are:
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 you want to extract the TicTacToe class file and the cross.gif image file. To do so, you can use this command:
jar xf TicTacToe.jar TicTacToe.class images/cross.gif
Updating a JAR File
The Jar tool 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:
- The u option indicates that you want to update an existing JAR file.
- The f option indicates that the JAR file to update is specified on the command line.
- jar-file is the existing JAR file that's to be updated.
- input-file(s) is a space-deliminated list of one or more files that you want to add to the Jar file.
Any files already in the archive having the same pathname as a file being added will be overwritten.
When creating a new JAR file, you can optionally use the -C option to indicate a change of directory.
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:
Running JAR-Packaged Software
Now that you've learned how to create JAR files, how do you actually run the code that you've packaged? Consider these three scenarios:
- Your JAR file contains an applet that is to be run inside a browser.
- Your JAR file contains an application that is to be invoked from the command line.
- Your JAR file contains code that you want to use as an extension.
This section will cover the first two situations. A separate trail in the tutorial on the extension mechanism covers the use of JAR files as extensions.
Applets Packaged in JAR Files
To invoke any applet from an HTML file for running inside a browser, you need to use the APPLET tag. For more information, see the Java Applets lesson. If the applet is bundled as a JAR file, the only thing you need to do differently is to use the ARCHIVE parameter to specify the relative path to the JAR file.
As an example, let's use (again!) the TicTacToe demo applet that ships with the Java™ Development Kit. The APPLET tag in the HTML file that calls the demo looks like this:
<applet code=TicTacToe.class
width=120 height=120></applet>
If the TicTacToe demo were packaged in a JAR file named TicTacToe.jar, you could modify the APPLET tag with the simple addition of an ARCHIVE parameter:
<applet code=TicTacToe.class
archive="TicTacToe.jar" width=120 height=120></applet>
The ARCHIVE parameter specifies the relative path to the JAR file that contains TicTacToe.class. This example assumes that the JAR file and the HTML file are in the same directory. If they're not, you would need to include the JAR file's relative path in the ARCHIVE parameter's value. For example, if the JAR file was one directory below the HTML file in a directory called applets, the APPLET tag would look like this:
<applet code=TicTacToe.class
archive="applets/TicTacToe.jar" width=120 height=120></applet>
JAR Files as Applications
You can run JAR-packaged applications with the Java interpreter. The basic command is:
java -jar jar-file
The -jar flag tells the interpreter that the application is packaged in the JAR file format. You can only specify one JAR file, which must contain all the application-specific code.
Before you execute this command make sure the runtime environment has an information of which class within the JAR file is the application's entry point.
To indicate which class is the application's entry point, you must add a Main-Class header to the JAR file's manifest. The header takes the form:
Main-Class: classname
The header's value, classname, is the name of the class that's the application's entry point.
When the Main-Class is set in the manifest file, you can run the application from the command line:
java -jar app.jar
To run the application from jar file that is in other directory, we need to
specify the path of that directory as below: java
-jar path/app.jar
where path is the
directory path at which this app.jar
resides.