jar - 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] ...
The 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 (not supported since JDK 11) or applications; however, beginning with JDK 9, users can use the jar command to create modular JARs. For transportation and deployment, it's 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.
When modules or the components of an 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's 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 valid only when creating or updating a modular jar or updating an existing non-modular jar:
Note:
All mandatory or optional arguments for long options are also mandatory or optional for any corresponding short options.
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.
You can use the following options to customize the actions of any operation mode included in the jar command.
jar [OPTION ...] [ [--release VERSION] [-C dir] files]
At run time, where more than one version of a class exists in the JAR, the JDK will use the first one it finds, searching initially in the directory tree whose VERSION number matches the JDK's major version number. It will then look in directories with successively lower VERSION numbers, and finally look in the root of the JAR.
You can use the following options to customize the actions of the create and the update main operation modes:
You can use the following options to customize the actions of the create (-c or --create) the update (-u or --update ) and the generate-index (-i or --generate-index=FILE) main operation modes:
The following options are recognized by the jar command and not used with operation modes:
jar --create --file classes.jar Foo.class Bar.class
jar --create --file classes.jar --manifest mymanifest -C foo/
jar --create --file foo.jar --main-class com.foo.Main --module-version 1.0 -C foo/classes resources
jar --update --file foo.jar --main-class com.foo.Main --module-version 1.0 -C foo/module-info.class
In this example, the classes/com/foo directory contains two classes, com.foo.Hello (the entry point class) and com.foo.NameProvider, both compiled for JDK 8. The classes-10/com/foo directory contains a different version of the com.foo.NameProvider class, this one containing JDK 10 specific code and compiled for JDK 10.
Given this setup, create a multirelease JAR file foo.jar by running the following command from the directory containing the directories classes and classes-10 .
jar --create --file foo.jar --main-class com.foo.Hello -C classes . --release 10 -C classes-10 .
The JAR file foo.jar now contains:
% jar -tf foo.jar META-INF/ META-INF/MANIFEST.MF com/ com/foo/ com/foo/Hello.class com/foo/NameProvider.class META-INF/versions/10/com/ META-INF/versions/10/com/foo/ META-INF/versions/10/com/foo/NameProvider.class
As well as other information, the file META-INF/MANIFEST.MF, will contain the following lines to indicate that this is a multirelease JAR file with an entry point of com.foo.Hello.
... Main-Class: com.foo.Hello Multi-Release: true
Assuming that the com.foo.Hello class calls a method on the com.foo.NameProvider class, running the program using JDK 10 will ensure that the com.foo.NameProvider class is the one in META-INF/versions/10/com/foo/. Running the program using JDK 8 will ensure that the com.foo.NameProvider class is the one at the root of the JAR, in com/foo.
Note:
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