Combines multiple files into a single JAR archive file.
jar [ options ] [manifest] destination input-file [input-files]
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.The 3 types of input files for the jar tool are
Typical usage is
- manifest file (optional)
- destination jar file
- files to be archived
% jar cf myjarfile *.classIn this example, all the class files in the current directory are placed into the file named "myjarfile". 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.If 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:
% jar cmf myManifestFile myJarFile *.classBe 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 cfm myJarFile myManifestFile *.classThe manifest uses RFC822 ascii format, so it is easy to view and process manifest-file contents.Beginning with version 1.3 of the Java 2 SDK, the jar utility supports JarIndex, which allows application class loaders to load classes 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 new -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, and INDEX.LIST file is inserted into the META-INF directory of main.jar's manifest.
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.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.
If any of "files" is a directory, then that directory is processed recursively.
- c
- Creates a new or empty archive on the standard output.
- t
- Lists the table of contents from standard output.
- x file
- Extracts all files, or just the named files, from standard input. If file is omitted, then all files are extracted; otherwise, only the specified file or files are extracted.
- f
- The second argument specifies a jar file to process. In the case of creation, this refers to the name of the jar file to be created (instead of on stdout). For table or xtract, the second argument identifies the jar file to be listed or extracted.
- v
- Generates verbose output on stderr.
- m
- Includes manifest information from specified pre-existing manifest file. 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 web page for examples of using the m option.jar cmf myManifestFile myJarFile *.class- 0
- Store only, 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, andjar uf foo.jar foo.classwould update foo.jar's 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.
- -C
- Temporaily changes directories during execution of jar command while processing the next 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, but not the classes directory itself, and then change to the bin directory and add xyz.class to foo.jar.jar uf foo.jar -C classes . -C bin xyz.class- -Joption
- Pass option to the Java virtual machine, 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 virtual machine.
To add all the files in a particular directory to an archive (overwriting contents if the archive already exists):$ ls 0.au 3.au 6.au 9.au at_work.gif 1.au 4.au 7.au Animator.class monkey.jpg 2.au 5.au 8.au Wave.class spacemusic.au $ jar cvf bundle.jar * adding: 0.au adding: 1.au adding: 2.au adding: 3.au adding: 4.au adding: 5.au adding: 6.au adding: 7.au adding: 8.au adding: 9.au adding: Animator.class adding: Wave.class adding: at_work.gif adding: monkey.jpg adding: spacemusic.au $If you already have subdirectories for images, audio files and classes in your html directory, I might jar up each directory into a single jar file:$ ls audio classes images $ jar cvf bundle.jar audio classes images adding: audio/1.au adding: audio/2.au adding: audio/3.au adding: audio/spacemusic.au adding: classes/Animator.class adding: classes/Wave.class adding: images/monkey.jpg adding: images/at_work.gif $ ls -l total 142 drwxr-xr-x 2 brown green 512 Aug 1 22:33 audio -rw-r--r-- 1 brown green 68677 Aug 1 22:36 bundle.jar drwxr-xr-x 2 brown green 512 Aug 1 22:26 classes drwxr-xr-x 2 brown green 512 Aug 1 22:25 images $You can then see the entry names in the jarfile using the jar tool and the "t" option:$ ls audio bundle.jar classes images $ jar tf bundle.jar 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.gif $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:$ jar tvf bundle.jar 145 Thu Aug 01 22:27:00 PDT 1996 META-INF/MANIFEST.MF 946 Thu Aug 01 22:24:22 PDT 1996 audio/1.au 1039 Thu Aug 01 22:24:22 PDT 1996 audio/2.au 993 Thu Aug 01 22:24:22 PDT 1996 audio/3.au 48072 Thu Aug 01 22:24:23 PDT 1996 audio/spacemusic.au 16711 Thu Aug 01 22:25:50 PDT 1996 classes/Animator.class 3368 Thu Aug 01 22:26:02 PDT 1996 classes/Wave.class 12809 Thu Aug 01 22:24:48 PDT 1996 images/monkey.jpg 527 Thu Aug 01 22:25:20 PDT 1996 images/at_work.gif $If you bundled a stock trade application(applet) into the following jar files:
main.jar buy.jar sell.jar other.jarand you specified in the Class-Path attribute in main.jar's manifest as:Class-Path: buy.jar sell.jar other.jarThen you can use the -i 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 right jar files when it is searching for classes or resources.SEE ALSO
The Jar Overview
The Jar File Specification
The JarIndex Spec
Java Tutorial on the Java Software web site.