JAR(1)
๐ซถ JAR(1) โ JDK Commands, but make it bestie ๐๐ซถ
โจ NAME โจ
jar โ create an archive for classes and resources, and manipulate or restore individual classes or resources from an archive. ๐
๐ธ SYNOPSIS ๐ธ
jar [OPTION ...] [ [--release VERSION] [-C dir] files] ...
๐ DESCRIPTION ๐
okay so 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. 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. 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:
--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.
๐ 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. periodt.
-c,--createโ Creates the archive.-i FILE,--generate-index=FILEโ Generates index information for the specified JAR file. This option is deprecated and may be removed in a future release. ๐จ-t,--listโ Lists the table of contents for the archive.-u,--updateโ Updates an existing JAR file.-x,--extractโ Extracts the named (or all) files from the archive.-d,--describe-moduleโ Prints the module descriptor or automatic module name.
๐ 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.
-C DIRโ Changes the specified directory and includes the files specified at the end of the command line.
jar [OPTION ...] [ [--release VERSION] [-C dir] files]
-f FILE,--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 namedMETA-INF/versions/VERSION/, whereVERSIONmust be must be a positive integer whose value is9or 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. highkey iconic.
-v,--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,--main-class=CLASSNAMEโ Specifies the application entry point for standalone applications bundled into a modular or executable modular JAR file.-m FILE,--manifest=FILEโ Includes the manifest information from the given manifest file.-M,--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,--module-pathโ Specifies the location of module dependence for generating the hash.@fileโ Readsjaroptions 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,--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".
โญ OTHER OPTIONS โญ
The following options are recognized by the jar command and not used with operation modes:
-h,--help[:compat]โ Displays the command-line help for thejarcommand or optionally the compatibility help.--help-extraโ Displays help on extra options.--versionโ Prints the program version.
๐ EXAMPLES OF JAR COMMAND SYNTAX ๐
- Create an archive,
classes.jar, that contains two class files,Foo.classandBar.class.
jar --create --file classes.jar Foo.class Bar.class
- Create an archive,
classes.jar, that contains two class files,Foo.classandBar.classsetting 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
- Create an archive,
classes.jar, by using an existing manifest,mymanifest, that contains all of the files in the directoryfoo/.
jar --create --file classes.jar --manifest mymanifest -C foo/
- Create a modular JAR archive,
foo.jar, where the module descriptor is located inclasses/module-info.class.
jar --create --file foo.jar --main-class com.foo.Main --module-version 1.0 -C foo/classes resources
- 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
- Create a versioned or multi-release JAR,
foo.jar, that places the files in theclassesdirectory at the root of the JAR, and the files in theclasses-10directory in theMETA-INF/versions/10directory 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.
- Create an archive,
my.jar, by reading options and lists of class files from the fileclasses.list.
Note: To shorten or simplify thejarcommand, you can provide an arg file that lists the files to include in the JAR file and pass it to thejarcommand with the at sign (@) as a prefix.
jar --create --file my.jar @classes.list
If one or more entries in the arg file cannot be found then the jar command fails without creating the JAR file.
okay bestie, that's the whole jar(1) page โ go make an archive that slays ๐
โจ. This one is from JDK 21, dated 2023. mwah ๐ซถ๐
๐ฆ where this page comes from
- opensuse-leap 16.0: java-21-openj9-devel 21.0.11.0-bp160.1.1 ยท Apache-1.1 AND Apache-2.0 AND EPL-2.0 AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-only WITH Classpath-exception-2.0 AND LGPL-2.0-only AND MPL-1.0 AND MPL-1.1 AND LicenseRef-SUSE-Public-Domain AND W3C
- opensuse-leap 16.0: java-21-openj9-devel 21.0.11.0-bp160.1.1 ยท Apache-1.1 AND Apache-2.0 AND EPL-2.0 AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-only WITH Classpath-exception-2.0 AND LGPL-2.0-only AND MPL-1.0 AND MPL-1.1 AND LicenseRef-SUSE-Public-Domain AND W3C
- opensuse-leap 16.0: java-21-openj9-devel 21.0.11.0-bp160.1.1 ยท Apache-1.1 AND Apache-2.0 AND EPL-2.0 AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-only WITH Classpath-exception-2.0 AND LGPL-2.0-only AND MPL-1.0 AND MPL-1.1 AND LicenseRef-SUSE-Public-Domain AND W3C
- opensuse-leap 16.0: java-21-openj9-devel 21.0.11.0-bp160.1.1 ยท Apache-1.1 AND Apache-2.0 AND EPL-2.0 AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-only WITH Classpath-exception-2.0 AND LGPL-2.0-only AND MPL-1.0 AND MPL-1.1 AND LicenseRef-SUSE-Public-Domain AND W3C
- Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms