You can use the
jar command to create an archive for classes and resources, and manipulate or restore individual classes or resources from an archive.
jar [OPTION...] [ [--release VERSION] [-C dir] files] ...
jar command is a general-purpose archiving and compression tool, based on the ZIP and ZLIB compression formats. Initially, the
jar command was designed to package Java applets or applications; however, beginning with JDK 9, users can use the
jar command to create modular JARs. For transportation and deployment, it is usually more convenient to package modules as modular JARs.
The syntax for the
jar command resembles the syntax for the
tar command. It has several main operation modes, defined by one of the mandatory operation arguments. Other arguments are either options that modify the behavior of the operation or are required to perform the operation.
Note:Although available and supported in JDK 9, the Applet API is marked as deprecated in preparation for removal in a future release. Instead of applets, consider alternatives such as Java Web Start or self-contained applications.
When modules or the components of an applet or application (files, images and sounds) are combined into a single archive, they can be downloaded by a Java agent (such as a browser) in a single HTTP transaction, rather than requiring a new connection for each piece. This dramatically improves download times. The
jar command also compresses files, which further improves download time. The
jar command also enables individual entries in a file to be signed so that their origin can be authenticated. A JAR file can be used as a class path entry, whether or not it is compressed.
An archive becomes a modular JAR when you include a module descriptor,
module-info.class, in the root of the given directories or in the root of the
.jar archive. The following operations described in Operation Modifiers Valid Only in Create and Update Modes are only valid when creating a modular jar or updating an existing non-modular jar:
All mandatory or optional arguments for long options are also mandatory or optional for any corresponding short options.
Main Operation Modes
When using the
jar command, you must specify the operation for it to perform. You specify the operation mode for the
jar command by including the appropriate operation arguments described in this section. You can mix an operation argument with other one-letter options. Generally the operation argument is the first argument specified on the command line.
Creates the archive.
Generates index information for the specified JAR archive.
Lists the table of contents for the archive.
Updates an existing JAR archive.
Extracts the named (or all) files from the archive.
Prints the module descriptor.
Operation Modifiers Valid in any Mode
You can use the following options to customize the actions of any operation mode included in the
Changes to the specified directory and includes the files specified at the end of the command line.
jar [OPTION...] [ [--release VERSION] [-C dir] files]
Specifies the archive file name.
Used to create a multi-release JAR file. Places all files specified at the end of the command line into a versioned directory of the JAR file (such as,
Sends or prints verbose output to standard output.
Operation Modifiers Valid Only in Create and Update Modes
You can use the following options to customize the actions of the create and the update main operation modes:
Specifies the application entry point for stand-alone applications bundled into a modular or executable modular JAR file.
Includes the manifest information from the given manifest file.
Doesn’t create a manifest file for the entries.
Specifies the module version, when creating a modular JAR file, or updating a non-modular JAR file.
Computes and records the hashes of modules matched by the given pattern and that depend upon directly or indirectly on a modular JAR file being created or a non-modular JAR file being updated.
Specifies the location of module dependence for generating the hash.
jar options and filenames from a text file.
Operation Modifiers Valid Only in Create, Update, and Generate-index Modes
You can use the following options to customize the actions of the create (
--create) the update (
--update ) and the generate-index (
--generate-index=FILE ) main operation modes.
The following options are recognized by the
jar command and not used with operation modes.
Examples of jar Command Syntax
classes.jar, that contains two class files,
jar --create --file classes.jar Foo.class Bar.class
classes.jar, by using an existing manifest,
mymanifest, that contains all of the files in the directory
jar --create --file classes.jar --manifest mymanifest -C foo/
foo.jar, where the module descriptor is located in
jar --create --file foo.jar --main-class com.foo.Main --module-version 1.0 -C foo/classes resources
foo.jar, to a modular JAR file.
jar --update --file foo.jar --main-class com.foo.Main --module-version 1.0 -C foo/module-info.class
foo.jar, that places files in the
jar --create --file my.jar -C foo classes --release 9 -C foo9 classes
my.jar, by reading options and lists of class files from the file
To shorten or simplify the
jar command, you can specify arguments in a separate text file and pass it to the
jar command with the at sign (
@) as a prefix.
jar --create --file my.jar @classes.list