JAR(1)

๐Ÿ“ฆ
version 27.0.0.0.35 ยท 37 versions
, in epel 10, epel 9

๐Ÿ’… JAR(1) โ€” JDK Commands ๐Ÿซถ

okay bestie, welcome to the jar page, she's a lot, but she's also iconic. This one's from the JDK Commands collection btw. Grab a strawberry drink ๐Ÿ“ and let's get into it.


โœจ NAME โœจ

jar โ€” create an archive for classes and resources, and manipulate or restore individual classes or resources from an archive. that's the whole vibe, no cap ๐Ÿ’–


๐ŸŒˆ SYNOPSIS ๐ŸŒˆ

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

literally that's it. she's giving minimalism โœจ


๐ŸŽ€ DESCRIPTION ๐ŸŽ€

okay so the jar command is a general-purpose archiving and compression tool, based on the ZIP and ZLIB compression formats. iconic behavior.

back in the day, jar was designed to package Java applets (which, ngl, are not supported since JDK 11 ๐Ÿ˜ญ rip bozo) or applications. BUT โ€” 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. periodt.

the syntax for jar kinda resembles the syntax for the tar command โ€” iykyk. it has several main operation modes, defined by one of the mandatory operation arguments. the other arguments are either options that modify the behavior of the operation, or they're required to actually perform the operation.

when modules or the components of an application (files, images and sounds) get combined into a single archive, they can be downloaded by a Java agent (like a browser bestie) in a single HTTP transaction, instead of requiring a whole new connection for each piece. this dramatically improves download times ๐Ÿ”ฅ. the jar command also compresses files, which further improves download time. and it 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. slay.

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. so like, don't think you can skip out on them just because you went short-form.

๐Ÿ‘‘ 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 ๐ŸŽ€. generally the operation argument is the first argument specified on the command line. main character energy fr.

  • -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 ๐Ÿ’€
  • -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. and listen: 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. versatile queens 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. and heads up: unlike with the create operation mode, this option can be specified only once with the extract operation mode ๐Ÿ™ˆ

  • -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 ๐Ÿ”ข

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. she's thorough ๐Ÿ’…

  • -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:

  • -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. not me skipping the manifest ๐Ÿ™ˆ
  • *--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:

  • -0 or --no-compress โ€” stores without using ZIP compression, zero thoughts head empty ๐Ÿซง
  • *--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 โ€” so like, watch out ๐Ÿ‘€

๐ŸŒŸ OTHER OPTIONS ๐ŸŒŸ

the following options are recognized by the jar command and are not used with operation modes:

  • -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 ๐Ÿ”ฅ

so as a JAR file is based on the ZIP format, it's 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. no shortcuts bestie, you gotta go back to the source ๐Ÿซถ

๐ŸŒธ EXAMPLES OF JAR COMMAND SYNTAX ๐ŸŒธ

ok bestie, this is the part where it all clicks โœจ here's every single example from the manpage, in order, with the commands untouched because those are sacred ๐Ÿ’…


  • Example one: the classic baby's first JAR ๐ŸŽ€ โ€” create an archive, classes.jar, that contains two class files, Foo.class and Bar.class.
jar --create --file classes.jar Foo.class Bar.class
  • Example two: same thing but with a timestamp, because we're fancy now ๐Ÿฆ‹ โ€” 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
  • Example three: manifest moment ๐Ÿ‘‘ โ€” 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/
  • Example four: going modular, we love a glow-up ๐Ÿ’– โ€” 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
  • Example five: the update arc, character development fr ๐ŸŒˆ โ€” 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
  • Example six: the multi-release one, buckle up bestie ๐Ÿ˜ญ โ€” 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.

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.

ngl this is the whole point of multi-release JARs and it ate โ€” same class name, different JDK versions, the right one gets picked. periodt. ๐Ÿ’…
  • Example seven: the arg file hack, iykyk ๐Ÿงš โ€” create an archive, my.jar, by reading options and lists of class files from the file 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.

jar --create --file my.jar @classes.list
โš ๏ธ 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-finished JARs, no crumbs, just a hard fail ๐Ÿ’€
  • Example eight: extraction station, we're out ๐ŸŒธ โ€” 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/

and that's the whole page, bestie ๐Ÿซถ you literally know jar now. this one's from the JDK 27-ea era, dated 2026, and it lives in section 1 of the manual โ€” go forth and archive responsibly, mwah ๐Ÿ’œ๐ŸŽ€

๐Ÿ“ฆ where this page comes from
  • epel 10: java-latest-openjdk-devel 1:27.0.0.0.35-0.0.1.0.ea.el10_3 ยท Apache-1.1 AND Apache-2.0 AND LicenseRef-Callaway-BSD AND LicenseRef-Callaway-BSD-with-advertising AND GPL-1.0-or-later AND GPL-2.0-only AND LicenseRef-Callaway-GPLv2-with-exceptions AND IJG AND LicenseRef-Callaway-LGPLv2+ AND LicenseRef-Callaway-MIT AND MPL-2.0 AND LicenseRef-Callaway-Public-Domain AND W3C AND Zlib AND ISC AND FTL AND LicenseRef-RSA
  • epel 10: java-latest-openjdk-devel-fastdebug 1:27.0.0.0.35-0.0.1.0.ea.el10_3 ยท Apache-1.1 AND Apache-2.0 AND LicenseRef-Callaway-BSD AND LicenseRef-Callaway-BSD-with-advertising AND GPL-1.0-or-later AND GPL-2.0-only AND LicenseRef-Callaway-GPLv2-with-exceptions AND IJG AND LicenseRef-Callaway-LGPLv2+ AND LicenseRef-Callaway-MIT AND MPL-2.0 AND LicenseRef-Callaway-Public-Domain AND W3C AND Zlib AND ISC AND FTL AND LicenseRef-RSA
  • epel 10: java-latest-openjdk-devel-slowdebug 1:27.0.0.0.35-0.0.1.0.ea.el10_3 ยท Apache-1.1 AND Apache-2.0 AND LicenseRef-Callaway-BSD AND LicenseRef-Callaway-BSD-with-advertising AND GPL-1.0-or-later AND GPL-2.0-only AND LicenseRef-Callaway-GPLv2-with-exceptions AND IJG AND LicenseRef-Callaway-LGPLv2+ AND LicenseRef-Callaway-MIT AND MPL-2.0 AND LicenseRef-Callaway-Public-Domain AND W3C AND Zlib AND ISC AND FTL AND LicenseRef-RSA
  • epel 9: java-latest-openjdk-devel 1:27.0.0.0.35-0.0.1.0.ea.el9 ยท Apache-1.1 AND Apache-2.0 AND LicenseRef-Callaway-BSD AND LicenseRef-Callaway-BSD-with-advertising AND GPL-1.0-or-later AND GPL-2.0-only AND LicenseRef-Callaway-GPLv2-with-exceptions AND IJG AND LicenseRef-Callaway-LGPLv2+ AND LicenseRef-Callaway-MIT AND MPL-2.0 AND LicenseRef-Callaway-Public-Domain AND W3C AND Zlib AND ISC AND FTL AND LicenseRef-RSA
  • epel 9: java-latest-openjdk-devel-fastdebug 1:27.0.0.0.35-0.0.1.0.ea.el9 ยท Apache-1.1 AND Apache-2.0 AND LicenseRef-Callaway-BSD AND LicenseRef-Callaway-BSD-with-advertising AND GPL-1.0-or-later AND GPL-2.0-only AND LicenseRef-Callaway-GPLv2-with-exceptions AND IJG AND LicenseRef-Callaway-LGPLv2+ AND LicenseRef-Callaway-MIT AND MPL-2.0 AND LicenseRef-Callaway-Public-Domain AND W3C AND Zlib AND ISC AND FTL AND LicenseRef-RSA
  • epel 9: java-latest-openjdk-devel-slowdebug 1:27.0.0.0.35-0.0.1.0.ea.el9 ยท Apache-1.1 AND Apache-2.0 AND LicenseRef-Callaway-BSD AND LicenseRef-Callaway-BSD-with-advertising AND GPL-1.0-or-later AND GPL-2.0-only AND LicenseRef-Callaway-GPLv2-with-exceptions AND IJG AND LicenseRef-Callaway-LGPLv2+ AND LicenseRef-Callaway-MIT AND MPL-2.0 AND LicenseRef-Callaway-Public-Domain AND W3C AND Zlib AND ISC AND FTL AND LicenseRef-RSA
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms