JAR(1)

πŸ“¦ version 27.0.1.0.3.u1, in arch rolling

πŸ¦‹ JAR(1) πŸ¦‹

okay bestie, buckle up πŸ’… this is jar β€” straight out of the JDK Commands corner of section 1 β€” and she's lowkey a whole icon. she creates archives for classes and resources, and she can also manipulate or restore individual classes or resources from an archive. main character energy fr fr πŸ‘‘


✨ SYNOPSIS ✨

jar [OPTION ...]  [ [--release VERSION] [-C dir] files] ...

like, that's the whole vibe check at the top. options first, then optionally a --release VERSION and -C dir before your files, and the ... means you can keep going 🫢


πŸ’– DESCRIPTION πŸ’–

so the jar command is a general-purpose archiving and compression tool, based on the ZIP and ZLIB compression formats. not me being versatile 😭

originally the jar command was designed to package Java applets β€” and ngl those are not supported since JDK 11 πŸ’€ β€” or applications. but here's the glow-up: beginning with JDK 9, users can use the jar command to create modular JARs. and for transportation and deployment, it's usually more convenient to package modules as modular JARs. slay.

the syntax for the jar command resembles the syntax for the tar command. she 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. no cap πŸŽ€

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. that dramatically improves download times, periodt. the jar command also compresses files, which further improves download time. she also enables individual entries in a file to be signed so that their origin can be authenticated πŸ”. and a JAR file can be used as a class path entry, whether or not it's compressed. versatile queen 🌈

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:

  • --module-version
  • --hash-modules
  • --module-path
πŸ“Œ Note: all mandatory or optional arguments for long options are also mandatory or optional for any corresponding short options. iykyk ✨

πŸ‘‘ MAIN OPERATION MODES πŸ‘‘

when using the jar command, you must specify the operation for it to perform. you specify the operation mode by including the appropriate operation arguments described in this section. you can mix an operation argument with other one-letter options β€” and generally the operation argument is the first argument specified on the command line. first-argument energy, we love to see it πŸ’…

  • -c or --create β€” creates the archive πŸŽ€
  • -i FILE or --generate-index=FILE β€” generates index information for the specified JAR file. ⚠️ this option is deprecated and may be removed in a future release β€” she's on her way out bestie, don't get attached 😭
  • -t or --list β€” lists the table of contents for the archive πŸ“‹
  • -u or --update β€” updates an existing JAR file πŸ”„
  • -x or --extract β€” extracts the named (or all) files from the archive. heads up: if a file with the same name appears more than once in the archive, each copy will be extracted, with later copies overwriting (replacing) earlier copies unless -k is specified πŸ™ˆ
  • -d or --describe-module β€” prints the module descriptor or automatic module name 🧚
  • --validate β€” validate the contents of the JAR file. see the Integrity of a JAR File section below for more details πŸ”

πŸŽ€ OPERATION MODIFIERS VALID IN ANY MODE πŸŽ€

you can use the following options to customize the actions of any operation mode included in the jar command. universal icons only πŸ’œ

  • -C DIR β€” when used with the create operation mode, changes the specified directory and includes the files specified at the end of the command line ✨
jar [OPTION ...]  [ [--release VERSION] [-C dir] files]

when used with the extract operation mode, it specifies the destination directory where the JAR file will be extracted. unlike with the create operation mode, this option can be specified only once with the extract operation mode. one (1) -C for extract, that's the rule 🫢

  • -f FILE or --file=FILE β€” specifies the archive file name πŸ“
  • --release VERSION β€” creates a multirelease JAR file. places all files specified after the option into a versioned directory of the JAR file named META-INF/versions/VERSION/, where VERSION must be a positive integer whose value is 9 or greater. and 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. newest-first, then root, iconic search behavior ⭐
  • -v or --verbose β€” 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. create/update exclusives, very VIP πŸ’…

  • -e CLASSNAME or --main-class=CLASSNAME β€” specifies the application entry point for standalone applications bundled into a modular or executable modular JAR file πŸšͺ
  • -m FILE or --manifest=FILE β€” includes the manifest information from the given manifest file πŸ“œ
  • -M or --no-manifest β€” doesn't create a manifest file for the entries 🚫
  • --module-version=VERSION β€” specifies the module version, when creating or updating a modular JAR file, or updating a non-modular JAR file πŸ”’
  • --hash-modules=PATTERN β€” 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 πŸ”
  • -p or --module-path β€” specifies the location of module dependence for generating the hash πŸ—ΊοΈ
  • @file β€” reads jar options and file names from a text file as if they were supplied on the command line πŸ“„

πŸ¦‹ OPERATION MODIFIERS VALID ONLY IN CREATE, UPDATE, AND GENERATE-INDEX 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. trio-only club 😌

  • -0 or --no-compress β€” stores without using ZIP compression πŸ“¦
  • --date=TIMESTAMP β€” the timestamp in ISO-8601 extended offset date-time with optional time-zone format, to use for the timestamp of the entries, e.g. "2022-02-12T12:30:00-05:00" πŸ•°οΈ

πŸ“ OPERATION MODIFIERS VALID ONLY IN EXTRACT MODE πŸ“

  • --dir DIR β€” directory into which the JAR file will be extracted πŸ“‚
  • -k or --keep-old-files β€” do not overwrite existing files. if a Jar file entry with the same name exists in the target directory, the existing file will not be overwritten. as a result, if a file appears more than once in an archive, later copies will not overwrite earlier copies. also note that some file system can be case insensitive πŸ‘€ (yes bestie, Foo.txt and foo.txt might be the same girl)

πŸ’€ OTHER OPTIONS πŸ’€

the following options are recognized by the jar command and are not used with operation modes. they do their own thing πŸ’…

  • -h or --help[:compat] β€” displays the command-line help for the jar command, or optionally the compatibility help πŸ™‹
  • --help-extra β€” displays help on extra options βž•
  • --version β€” prints the program version 🏷️

πŸ”₯ INTEGRITY OF A JAR FILE πŸ”₯

as a JAR file is based on ZIP format, it is possible to create a JAR file using tools other than the jar command πŸ‘€. the --validate option may be used to perform the following integrity checks against a JAR file:

  • that there are no duplicate Zip entry file names 🚫
  • verify that the Zip entry file name:
    • is not an absolute path
    • the file name is not . or ..
    • does not contain a backslash, \
    • does not contain a drive letter
    • path element does not include . or ..
  • the API exported by a multi-release jar archive is consistent across all different release versions ✨

the jar tool exits with a status of 0 if there were no integrity issues encountered and >0 if an error/warning occurred 🚦

⚠️ when an integrity issue is reported, it will often require that the JAR file is re-created by the original source of the JAR file. like, you can't just patch the vibe, you gotta go back to the source bestie 😭

✨ EXAMPLES OF JAR COMMAND SYNTAX ✨

okay bestie, this is the part where it all clicks πŸ’– these are real, literal examples of the jar command doing its thing β€” copy-paste energy, no cap. Read them like a recipe book where every recipe is a slay.


  • Create an archive, classes.jar, that contains two class files, Foo.class and Bar.class.
jar --create --file classes.jar Foo.class Bar.class

literally the simplest glow-up ever πŸŽ€ two files go in, one JAR comes out.

  • Create an archive, classes.jar, that contains two class files, Foo.class and Bar.class setting the last modified date and time to 2021 Jan 6 12:36:00.
jar --create --date="2021-01-06T14:36:00+02:00" --file=classes.jar Foo.class Bar.class

the --date flag is giving time-travel main character energy ⏰ you're setting the last modified date and time to 2021 Jan 6 12:36:00, periodt.

  • Create an archive, classes.jar, by using an existing manifest, mymanifest, that contains all of the files in the directory foo/.
jar --create --file classes.jar --manifest mymanifest -C foo/

reusing a manifest you already made? iconic. πŸ’… and -C foo/ scoops up everything inside that directory.

  • Create a modular JAR archive, foo.jar, where the module descriptor is located in classes/module-info.class.
jar --create --file foo.jar --main-class com.foo.Main --module-version 1.0 -C foo/classes resources

modular JAR build, we love to see it 🌸 --main-class sets the entry point, --module-version stamps the version, and -C foo/classes resources pulls in that resources directory from over there.

  • Update an existing non-modular JAR, 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

glow-up arc fr fr β€” --update turns a plain non-modular JAR into a modular one, no re-build required πŸ¦‹

  • Create a versioned or multi-release JAR, foo.jar, that places the files in the classes directory at the root of the JAR, and the files in the classes-10 directory in the META-INF/versions/10 directory of the JAR.

okay this one is the boss level, but stay with me bestie, it's so worth it πŸ”₯

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

look at that tree 😭 it's beautiful. 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.

ngl this is the whole reason multi-release JARs exist β€” each JDK grabs the version of the class that was actually made for it. JDK 10 gets the spicy new one, JDK 8 gets the classic. everyone eats. πŸ“
  • Create an archive, my.jar, by reading options and lists of class files from the file classes.list.
jar --create --file my.jar @classes.list

Note:

To shorten or simplify the jar command, you can provide an arg file that lists the files to include in the JAR file and pass it to the jar command with the at sign (@) as a prefix.

⚠️ heads up bestie: if one or more entries in the arg file cannot be found then the jar command fails without creating the JAR file. no half-built JAR, no mess β€” it just straight up refuses. πŸ’€
  • Extract the JAR file foo.jar to /tmp/bar/ directory:
jar -xf foo.jar -C /tmp/bar/

Alternatively, you can also do:

jar --extract --file foo.jar --dir /tmp/bar/

two ways to do the exact same thing, and honestly? both slay. pick your fighter πŸ’œ


and that's the whole page, bestie!! you made it to the end, I'm so proud of you 😭🫢

this one's from JDK 27.0.1.0.3, dated 2026 β€” go forth and jar responsibly, you absolute icon πŸ‘‘βœ¨

πŸ“¦ where this page comes from