Combines multiple files into a single JAR archive file.
- Create jar file
jar c[v0M]f
jarfile[-C
dir]
inputfiles[-J
option]
jar c[v0]mf
manifestjarfile
[-C
dir]
inputfiles[-J
option]
jar c[v0M] [-C
dir]
inputfiles[-J
option]
jar c[v0]m
manifest[-C
dir]
inputfiles[-J
option]
- Update jar file
jar u[v0M]f
jarfile[-C
dir]
inputfiles[-J
option]
jar u[v0]mf
manifestjarfile
[-C
dir]
inputfiles[-J
option]
jar u[v0M] [-C
dir]
inputfiles[-J
option]
jar u[v0]m
manifest[-C
dir]
inputfiles[-J
option]
- Extract jar file
jar x[v]f
jarfile[
inputfiles] [-J
option]
jar x[v] [
inputfiles] [-J
option]
- List table of contents of jar file
jar t[v]f
jarfile[
inputfiles] [-J
option]
jar t[v] [
inputfiles] [-J
option]
- Add index to jar file
jar i
jarfile[-J
option]
where:
- jarfile
- Jar file to be created, updated, extracted, or have its table of contents viewed. This filename is required with the
f
option. Note that omittingf
accepts the jar file from standard input (for x and t) or sends the jar file to standard output (for c and u).- inputfiles
- Files or directories to be combined into a single jar file. Can use wildcard (*), and can supply multiples files and directories separated by spaces. Any directories are processed recursively. Note that a lone asterisk (*) passed in for inputfileshas the same effect as a lone dot (.). The files are compressed unless option
O
(zero) is used. Any inputfiles (non-jar files) supplied with x and t are ignored.- manifest
- Pre-existing manifest file to be included in the jar file, as MANIFEST.MF. This filename is required with the
m
option. The letters m and f must appear in the same order that manifest and jarfile appear.-J
option- Option to be passed into the Java runtime environment.
-C
dir- Temporarily changes directories to dir while processing the following inputfiles argument. Multiple
-C
dir inputfiles combinations are allowed.
The jar tool combines multiple files into a single JAR archive file. jar is a general-purpose archiving and compression tool, based on ZIP and the ZLIB compression format. However, jar was designed mainly to facilitate the packaging of java applets or applications into a single archive. When the components of an applet or application (.class files, images and sounds) are combined into a single archive, they may be downloaded by a java agent (like a browser) in a single HTTP transaction, rather than requiring a new connection for each piece. This dramatically improves download times. jar also compresses files and so further improves download time. In addition, it allows individual entries in a file to be signed by the applet author so that their origin can be authenticated. The syntax for the jar tool is almost identical to the syntax for the tar command. A jar archive can be use as a class path entry, whether it is compressed or not.Typical usage to combine files into a jar file is:
In this example, all the class files in the current directory are placed into the file named "myFile.jar". A manifest file is automatically generated by the jar tool and is always the first entry in the jar file. By default, it is named META-INF/MANIFEST.MF. The manifest file is the place where any meta-information about the archive is stored. Refer to the JAR file specification for details about how meta-information is stored in the manifest file.% jar cf myFile.jar *.classIf you have a pre-existing manifest file that you want the jar tool to use for the new jar archive, you can specify it using the
m
option:Be sure that any pre-existing manifest file that you use ends with a new line. The last line of a manifest file will not be parsed if it doesn't end with a new line character. Note that when you specify "cfm" instead of "cmf" (i.e., you invert the order of the "m" and "f" options), you need to specify the name of the jar archive first, followed by the name of the manifest file:% jar cmf myManifestFile myFile.jar *.classThe manifest uses RFC822 ascii format, so it is easy to view and process manifest-file contents.% jar cfm myFile.jar myManifestFile *.classTo extract the files from a jar file, use
x
, as in:% jar xf myFile.jarBeginning with version 1.3 of the Java 2 SDK, the jar utility supports JarIndex, which allows application class loaders to load classes more efficiently from jar files. If an application or applet is bundled into multiple jar files, only the necessary jar files will be downloaded and opened to load classes. This performance optimization is enabled by running jar with the i option. It will generate package location information for the specified main jar file and all the jar files it depends on, which need to be specified in the Class-Path attribute of the main jar file's manifest.
% jar i main.jarIn this example, an INDEX.LIST file is inserted into the META-INF directory of main.jar.
The application class loader will use the information stored in this file for efficient class loading. Refer to the JarIndex specification for details about how location information is stored in the index file.A standard way to copy directories is to first compress files in dir1 to standard out, then extract from standard in to dir2:
% (cd dir1; jar c .) | (cd dir2; jar x)Examples of using the Jar tool to operate on Jar files and Jar file manifests are provided below and in the Jar trail of the Java Tutorial.
- c
- Creates a new archive to jarfile or the standard output (if jarfile is omitted).
- t
- Lists the table of contents from jarfile or standard output (if jarfile is omitted).
- x
- Extracts the files from jarfile or standard input (if jarfile is omitted).
- f
- Specifies a jar file to be created, updated, extracted, indexed, or have its table of contents viewed.
- v
- Generates verbose output to stderr.
- m
- Includes manifest information from specified pre-existing manifest file, creating a file at META-INF/MANIFEST.MF. The letters m and f must appear in the same order that manifest and jarfile appear. Example use:
You can add special-purpose name-value attribute headers to the manifest file that aren't contained in the default manifest. Examples of such headers would be those for vendor information, version information, package sealing, and headers to make JAR-bundled applications executable. See the JAR Files trail in the Java Tutorial and the Notes for Developers page for examples of using the m option.jar cmf myManifestFile myFile.jar *.class- 0
- (zero) Store without using ZIP compression.
- M
- Do not create a manifest file for the entries.
- u
- Update an existing JAR file by adding files or changing the manifest. For example:
would add the file foo.class to the existing JAR file foo.jar. Another example:jar uf foo.jar foo.classwould update the foo.jar manifest with the information in manifest.jar umf manifest foo.jar- i
- Generate index information for the specified jar file and its dependent jar files. For example:
jar i foo.jarwould generate an INDEX.LIST file in foo.jar which contains location information for each package in foo.jar and all the jar files specified in foo.jar's Class-Path attribute. See the index example.
- -C dir
- Temporarily changes directories (
cd
dir) during execution of the jar command while processing the following inputfiles argument. Its operation is intended to be similar to the -C option of the UNIX tar utility. For example:would change to the classes directory and add the bar.class from that directory to foo.jar. The following command,jar uf foo.jar -C classes bar.classwould change to the classes directory and add to foo.jar all files within the classes directory (without creating a classes directory in the jar file), then change back to the original directory before changing to the bin directory to add xyz.class to foo.jar. Ifjar uf foo.jar -C classes . -C bin xyz.classclasses
holds filesbar1
andbar2
, then here's what the jar file would contain usingjar tf foo.jar
:META-INF/ META-INF/MANIFEST.MF bar1 bar2 xyz.class-J
option- Pass option to the Java runtime environment, where option is one of the options described on the reference page for the java application launcher. For example,
-J-Xms48M
sets the startup memory to 48 megabytes. It is a common convention for -J to pass options to the underlying runtime environment.
To add all the files in a particular directory to an archive (overwriting contents if the archive already exists). Enumerating verbosely (with the "v" option) will tell you more information about the files in the archive, such as their size and last modified date.If you already have separate subdirectories for images, audio files and classes, you can combine them into a single jar file:% ls 1.au Animator.class monkey.jpg 2.au Wave.class spacemusic.au 3.au at_work.gif % jar cvf bundle.jar * added manifest adding: 1.au(in = 2324) (out= 67)(deflated 97%) adding: 2.au(in = 6970) (out= 90)(deflated 98%) adding: 3.au(in = 11616) (out= 108)(deflated 99%) adding: Animator.class(in = 2266) (out= 66)(deflated 97%) adding: Wave.class(in = 3778) (out= 81)(deflated 97%) adding: at_work.gif(in = 6621) (out= 89)(deflated 98%) adding: monkey.jpg(in = 7667) (out= 91)(deflated 98%) adding: spacemusic.au(in = 3079) (out= 73)(deflated 97%)To see the entry names in the jarfile, use the "t" option:% ls -F audio/ classes/ images/ % jar cvf bundle.jar audio classes images added manifest adding: audio/(in = 0) (out= 0)(stored 0%) adding: audio/1.au(in = 2324) (out= 67)(deflated 97%) adding: audio/2.au(in = 6970) (out= 90)(deflated 98%) adding: audio/3.au(in = 11616) (out= 108)(deflated 99%) adding: audio/spacemusic.au(in = 3079) (out= 73)(deflated 97%) adding: classes/(in = 0) (out= 0)(stored 0%) adding: classes/Animator.class(in = 2266) (out= 66)(deflated 97%) adding: classes/Wave.class(in = 3778) (out= 81)(deflated 97%) adding: images/(in = 0) (out= 0)(stored 0%) adding: images/monkey.jpg(in = 7667) (out= 91)(deflated 98%) adding: images/at_work.gif(in = 6621) (out= 89)(deflated 98%) % ls -F audio/ bundle.jar classes/ images/% jar tf bundle.jar META-INF/ META-INF/MANIFEST.MF audio/1.au audio/2.au audio/3.au audio/spacemusic.au classes/Animator.class classes/Wave.class images/monkey.jpg images/at_work.gifTo add an index file to the jar file for speeding up class loading, use the "i" option.
Let's say you split the inter-dependent classes for a stock trade application, into three jar files: main.jar, buy.jar, and sell.jar. If you specify the Class-path attribute in the main.jar manifest as:Class-Path: buy.jar sell.jarthen you can use thei
option to speed up your application's class loading time:% jar i main.jarAn INDEX.LIST file is inserted to the META-INF directory which will enable the application class loader to download the specified jar files when it is searching for classes or resources.
The Jar Overview
The Jar File Specification
The JarIndex Spec
Java Tutorial on the Java Software web site.
Copyright © 2003 Sun Microsystems, Inc. All Rights Reserved. |
![]() Java Software |