Previous Page
Next Page

The jar Utility

The Java archive (JAR) file format enables you to bundle multiple files into a single archive file, much the same way you can bundle files by using the tar utility. Typically, a JAR file contains the class files and auxiliary resources associated with Java applets and applications.

The benefits of using the JAR file format include the following:

  • Security You can digitally sign the contents of a JAR file. Users who recognize your signature can then optionally grant your files security privileges that they 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 Hypertext Transfer Protocol (HTTP) transaction, without the need for opening a new connection for each file.

  • Compression The JAR format enables you to compress 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 optionally be sealed so that they can enforce version consistency. To seal 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 application programming interface (API).

The jar command is similar to the tar command in that it packages several files into a single file, but it also compresses the resulting file. It is a Java application that combines multiple files into a single JAR file. It is also a general-purpose archiving and compression tool that is based on Zip and the ZLIB compression format. The jar command was originally created so that Java programmers could download multiple files with one request rather than having to issue a download request for each separate file. jar is standard with the Solaris 10 operating system, and it is also available on any system that has a Java Virtual Machine (JVM) installed.

This is the syntax for the jar command:

jar cf <jar-file> <input-file(s)>

Table 7.17 describes the options and arguments used with the jar command.

Table 7.17. jar Command Options




Indicates that you want to create a JAR file.[*]


Indicates that you want the output to go to a file rather than to the system's standard output.[*]


Generates index information for the JAR file(s).


Lists the table of contents for the archive.


Produces verbose output on standard output while the JAR file is being built. The verbose output tells you the name of each file as it is added to the JAR file.


Extracts files from an archive.


Indicates that you don't want the JAR file to be compressed.


Specifies 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, although that is not required.


Specifies a space-separated list of one or more files that you want to be placed in your JAR file. The <input-file(s)> argument can contain the wildcard asterisk (*) symbol. If input-files is a directory, the content of the directory is added to the JAR recursively.

[*] The c and f options can appear in either order, but there must not be any space between them.

You use the following to create a JAR file:

jar cf <jar-file> <input-file(s)>

You use the following to view the contents of a JAR file:

jar tf <jar-file>

You use the following to extract the contents of a JAR file:

jar xf <jar-file>

You use the following to extract specific files from a JAR file:

jar xf <jar-file> <archived-file(s)>

Here's an example of how to use jar to compress files located within two different directories. JAR files are packaged with the Zip file format, so you can use them for Zip-like tasks, such as lossless data compression, archiving, decompression, and archive unpacking. To package the audio and images directories into a single JAR file named files.jar in your default home directory, you would run the following command from inside the /export/home/bcalkins directory:

jar cvf ~/files.jar files.class audio images

The audio and images arguments represent directories, so the JAR tool recursively places them and their contents in the JAR file. The generated JAR file files.jar is placed in the user's home directory. Because the command used the v option for verbose output, you see something similar to this output when you run the command:

adding: files.class (in=3825) (out=2222) (deflated 41%)
adding: audio/ (in=0) (out=0) (stored 0%)
adding: audio/ (in=4032) (out=3572) (deflated 11%)
adding: audio/ (in=2566) (out=2055) (deflated 19%)
adding: audio/ (in=6558) (out=4401) (deflated 32%)
adding: audio/ (in=7834) (out=6985) (deflated 10%)
adding: audio/ (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 files.jar is compressed. The JAR tool compresses files by default. You can turn off the compression feature by using the 0 option; in that case, the command looks like this:

jar cvf0 files.jar files.class audio images

Previous Page
Next Page