jar c[v0M]f
jarfile
[-C
dir]
inputfiles
[-J
option]
jar c[v0]mf
manifest
jarfile [-C
dir]
inputfiles
[-J
option]
[-e entrypoint]jar c[v0M] [-C
dir]
inputfiles
[-J
option]
jar c[v0]m
manifest
[-C
dir]
inputfiles
[-J
option]
jar u[v0M]f
jarfile
[-C
dir]
inputfiles
[-J
option]
jar u[v0]mf
manifest
jarfile [-C
dir]
inputfiles
[-J
option]
[-e entrypoint]jar u[v0M] [-C
dir]
inputfiles
[-J
option]
jar u[v0]m
manifest
[-C
dir]
inputfiles
[-J
option]
jar x[v]f
jarfile
[
inputfiles]
[-J
option]
jar x[v] [
inputfiles]
[-J
option]
jar t[v]f
jarfile
[
inputfiles]
[-J
option]
jar t[v] [
inputfiles]
[-J
option]
jar i
jarfile
[-J
option]
where:
cuxtiv0Mmf
jar
command.c
), updated
(u
), extracted (x
), or have its
table of contents viewed (t
). The
-f option and filename jarfile are a
pair -- if either is present, they must both appear. Note
that omitting -f and jarfile accepts
jar file from standard input (for x and t) or sends
jar file to standard output (for c and u).MANIFEST.MF
in the jar file. The -m
option and filename manifest are a pair -- if
either is present, they must both appear. The letters
m and f must appear in the same order that
manifest and jarfile appear.-C
dirTypical usage to combine files into a jar file is:
C:\Java> jar cf myFile.jar *.classIn this example, all the class files in the current directory are placed into the file named "myFile.jar". A manifest file entry named META-INF/MANIFEST.MF is automatically generated by the jar tool and is always the first entry in the jar file. The manifest file is the place where any meta-information about the archive is stored as name : value pairs. 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 whose
name:
value pairs you want the
jar tool to include for the new jar archive, you can specify it
using the -m option:
C:\Java> jar cmf myManifestFile myFile.jar *.classAn existing manifest file must end with a new line character. jar does not parse the last line of a manifest file if it does not end with a new line character.
C:\Java> jar cfm myFile.jar myManifestFile *.classThe manifest is in a text format inspired by RFC822 ASCII format, so it is easy to view and process manifest-file contents.
To extract the files from a jar file, use -x, as in:
C:\Java> jar xf myFile.jar
To extract only certain files from a jar file, supply their filenames:
C:\Java> jar xf myFile.jar foo bar
Beginning with Java 2 SDK v1.3, 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.
C:\Java> jar i main.jar
In 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 (omitting f
from both jar
commands):
C:\Java> (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.
f
is
specified) or to standard output (if f
and
jarfile are omitted). Add to it the files and
directories specified by inputfiles.f
is
specified) by adding to it files and directories specified
by inputfiles. For
example:
jar uf foo.jar foo.classadds the file foo.class to the existing jar file foo.jar. The -u option can also update the manifest entry, as given by this example:
jar umf manifest foo.jarupdates the foo.jar manifest with the name
:
value pairs in
manifest.
f
is
specified) or standard input (if f
and
jarfile are omitted). If inputfiles is specified, only
those specified files and directories are extracted.
Otherwise, all files and directories are extracted.
The time and date of the extracted files are those
given in the archive.f
is
specified) or standard input (if f
and
jarfile are omitted). If inputfiles is specified, only
those specified files and directories are listed. Otherwise,
all files and directories are listed.jar i foo.jar
generates an INDEX.LIST
file in
foo.jar
which contains location information for
each package in foo.jar
and all the jar files specified in the Class-Path
attribute of
foo.jar
. See the index example.
c
), updated (u
), extracted
(x
), indexed (i
), or viewed
(t
). The -f option and filename
jarfile are a pair -- if present, they must both
appear. Omitting f
and jarfile accepts
a "jar file" from standard input (for x and t) or sends the
"jar file" to standard output (for c and u).:
value
attribute pairs from the specified manifest file
manifest in the file at
META-INF/MANIFEST.MF. A
name:
value pair is added
unless one already exists with the same name, in which case
its value is updated.
On the command line, the letters m and f must appear in the same order that manifest and jarfile appear. Example use:
jar cmf myManifestFile myFile.jar *.classYou can add special-purpose name : value attribute pairs to the manifest that aren't contained in the default manifest. For example, you can add attributes specifying vendor information, version information, package sealing, or to make JAR-bundled applications executable. See the JAR Files trail in the Java Tutorial for examples of using the -m option.
Main.jar
where the Main-Class attribute value in the manifest is set to Main
:
jar cfe Main.jar Main Main.class
The java runtime can directly invoke this application by running the following command:
java -jar Main.jarIf the entrypoint class name is in a package it may use either a dot (".") or slash ("/") character as the delimiter. For example, if
Main.class
is in a package called
foo
the entry point can be specified in the following ways:
jar -cfe Main.jar foo/Main foo/Main.classor
jar -cfe Main.jar foo.Main foo/Main.classNote: specifying both -m and -e options together when the given manifest also contains the Main-Class attribute results in an ambigous
Main.class
specification, leading to an error and the jar creation or
update operation is aborted.
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:
jar uf foo.jar -C classes bar.classchanges to the classes directory and add the bar.class from that directory to foo.jar. The following command,
jar uf foo.jar -C classes . -C bin xyz.classchanges to the classes directory and adds to foo.jar all files within the classes directory (without creating a classes directory in the jar file), then changes back to the original directory before changing to the bin directory to add xyz.class to foo.jar. If
classes
holds files bar1
and
bar2
, then here's what the jar file
contains using jar tf foo.jar
:
META-INF/
META-INF/MANIFEST.MF
bar1
bar2
xyz.class
-J
option-J-Xmx48M
sets the maximum memory to 48
megabytes. It is a common convention for -J to pass options
to the underlying runtime environment.jar
command (except -J options). This
enables you to create jar commands of any length, overcoming
command line limits imposed by the operating system.
An argument file can include options and filenames. The arguments within a file can be space-separated or newline-separated. Filenames within an argument file are relative to the current directory, not relative to the location of the argument file. Wildcards (*) that might otherwise be expanded by the operating system shell are not expanded. Use of the @ character to recursively interpret files is not supported. The -J options are not supported because they are passed to the launcher, which does not support argument files.
When executing jar, pass in the path and name of each
argument file with the @
leading character. When jar
encounters an argument beginning with the character
@
, it expands the contents of that file into the
argument list.
For example, you can use a single argument file named
classes.list
to hold the names of the files:
C:\Java> dir /b *.class > classes.list
Then execute the jar command passing in the argfile:
C:\Java> jar cf my.jar @classes.listAn argument file can be passed in with a path, but any filenames inside the argument file that have relative paths are relative to the current working directory, not the path passed in. Here's such an example:
C:\Java> jar @path1/classes.list
C:\Java> dir 12/09/96 12:20a <DIR> . 12/09/96 12:17a <DIR> .. 12/09/96 12:18a 946 1.au 12/09/96 12:18a 1,039 2.au 12/09/96 12:18a 993 3.au 12/09/96 12:19a 48,072 spacemusic.au 12/09/96 12:19a 527 at_work.gif 12/09/96 12:19a 12,818 monkey.jpg 12/09/96 12:19a 16,242 Animator.class 12/09/96 12:20a 3,368 Wave.class 10 File(s) 91,118 bytes C:\Java> jar cvf bundle.jar * adding manifest adding: 1.au adding: 2.au adding: 3.au adding: Animator.class adding: Wave.class adding: at_work.gif adding: monkey.jpg adding: spacemusic.au
If you already have separate subdirectories for images, audio files and classes, you can combine them into a single jar file:
C:\Java> dir 12/09/96 12:11a <DIR> . 12/09/96 12:17a <DIR> .. 12/03/96 06:54p <DIR> audio 12/06/96 02:02p <DIR> images 12/09/96 12:10a <DIR> classes 5 File(s) 207,360 bytes C:\Java> 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 C:\Java> dir 12/09/96 12:11a <DIR> . 12/09/96 12:17a <DIR> .. 12/09/96 12:11a 207,360 bundle.jar 12/03/96 06:54p <DIR> audio 12/06/96 02:02p <DIR> images 12/09/96 12:10a <DIR> classes 6 File(s) 207,360 bytes
To see the entry names in the jarfile, use the "t" option:
C:\Java> 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.gif
To 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 the -i option to speed up your application's class loading time:
C:\Java> 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.