1JAR(1) JDK Commands JAR(1)
2
3
4
6 jar - create an archive for classes and resources, and manipulate or
7 restore individual classes or resources from an archive
8
10 jar [OPTION ...] [ [--release VERSION] [-C dir] files] ...
11
13 The jar command is a general-purpose archiving and compression tool,
14 based on the ZIP and ZLIB compression formats. Initially, the jar com‐
15 mand was designed to package Java applets (not supported since JDK 11)
16 or applications; however, beginning with JDK 9, users can use the jar
17 command to create modular JARs. For transportation and deployment,
18 it's usually more convenient to package modules as modular JARs.
19
20 The syntax for the jar command resembles the syntax for the tar com‐
21 mand. It has several main operation modes, defined by one of the
22 mandatory operation arguments. Other arguments are either options that
23 modify the behavior of the operation or are required to perform the op‐
24 eration.
25
26 When modules or the components of an application (files, images and
27 sounds) are combined into a single archive, they can be downloaded by a
28 Java agent (such as a browser) in a single HTTP transaction, rather
29 than requiring a new connection for each piece. This dramatically im‐
30 proves download times. The jar command also compresses files, which
31 further improves download time. The jar command also enables individu‐
32 al entries in a file to be signed so that their origin can be authenti‐
33 cated. A JAR file can be used as a class path entry, whether or not
34 it's compressed.
35
36 An archive becomes a modular JAR when you include a module descriptor,
37 module-info.class, in the root of the given directories or in the root
38 of the .jar archive. The following operations described in Operation
39 Modifiers Valid Only in Create and Update Modes are valid only when
40 creating or updating a modular jar or updating an existing non-modular
41 jar:
42
43 • --module-version
44
45 • --hash-modules
46
47 • --module-path
48
49 Note:
50
51 All mandatory or optional arguments for long options are also mandatory
52 or optional for any corresponding short options.
53
55 When using the jar command, you must specify the operation for it to
56 perform. You specify the operation mode for the jar command by includ‐
57 ing the appropriate operation arguments described in this section. You
58 can mix an operation argument with other one-letter options. Generally
59 the operation argument is the first argument specified on the command
60 line.
61
62 -c or --create
63 Creates the archive.
64
65 -i=FILE or --generate-index=FILE
66 Generates index information for the specified JAR file.
67
68 -t or --list
69 Lists the table of contents for the archive.
70
71 -u or --update
72 Updates an existing JAR file.
73
74 -x or --extract
75 Extracts the named (or all) files from the archive.
76
77 -d or --describe-module
78 Prints the module descriptor or automatic module name.
79
81 You can use the following options to customize the actions of any oper‐
82 ation mode included in the jar command.
83
84 -C DIR Changes the specified directory and includes the files specified
85 at the end of the command line.
86
87 jar [OPTION ...] [ [--release VERSION] [-C dir] files]
88
89 -f=FILE or --file=FILE
90 Specifies the archive file name.
91
92 --release VERSION
93 Creates a multirelease JAR file. Places all files specified af‐
94 ter the option into a versioned directory of the JAR file named
95 META-INF/versions/VERSION/, where VERSION must be must be a pos‐
96 itive integer whose value is 9 or greater.
97
98 At run time, where more than one version of a class exists in
99 the JAR, the JDK will use the first one it finds, searching ini‐
100 tially in the directory tree whose VERSION number matches the
101 JDK's major version number. It will then look in directories
102 with successively lower VERSION numbers, and finally look in the
103 root of the JAR.
104
105 -v or --verbose
106 Sends or prints verbose output to standard output.
107
109 You can use the following options to customize the actions of the cre‐
110 ate and the update main operation modes:
111
112 -e=CLASSNAME or --main-class=CLASSNAME
113 Specifies the application entry point for standalone applica‐
114 tions bundled into a modular or executable modular JAR file.
115
116 -m=FILE or --manifest=FILE
117 Includes the manifest information from the given manifest file.
118
119 -M or --no-manifest
120 Doesn't create a manifest file for the entries.
121
122 --module-version=VERSION
123 Specifies the module version, when creating or updating a modu‐
124 lar JAR file, or updating a non-modular JAR file.
125
126 --hash-modules=PATTERN
127 Computes and records the hashes of modules matched by the given
128 pattern and that depend upon directly or indirectly on a modular
129 JAR file being created or a non-modular JAR file being updated.
130
131 -p or --module-path
132 Specifies the location of module dependence for generating the
133 hash.
134
135 @file Reads jar options and file names from a text file.
136
138 GENERATE-INDEX MODES
139
140 You can use the following options to customize the actions of the cre‐
141 ate (-c or --create) the update (-u or --update ) and the generate-in‐
142 dex (-i or --generate-index=FILE) main operation modes:
143
144 -0 or --no-compress
145 Stores without using ZIP compression.
146
148 The following options are recognized by the jar command and not used
149 with operation modes:
150
151 -h or --help[:compat]
152 Displays the command-line help for the jar command or optionally
153 the compatibility help.
154
155 --help-extra
156 Displays help on extra options.
157
158 --version
159 Prints the program version.
160
162 • Create an archive, classes.jar, that contains two class files,
163 Foo.class and Bar.class.
164
165 jar --create --file classes.jar Foo.class Bar.class
166
167 • Create an archive, classes.jar, by using an existing manifest, myman‐
168 ifest, that contains all of the files in the directory foo/.
169
170 jar --create --file classes.jar --manifest mymanifest -C foo/
171
172 • Create a modular JAR archive,foo.jar, where the module descriptor is
173 located in classes/module-info.class.
174
175 jar --create --file foo.jar --main-class com.foo.Main --mod‐
176 ule-version 1.0 -C foo/classes resources
177
178 • Update an existing non-modular JAR, foo.jar, to a modular JAR file.
179
180 jar --update --file foo.jar --main-class com.foo.Main --mod‐
181 ule-version 1.0 -C foo/module-info.class
182
183 • Create a versioned or multi-release JAR, foo.jar, that places the
184 files in the classes directory at the root of the JAR, and the files
185 in the classes-10 directory in the META-INF/versions/10 directory of
186 the JAR.
187
188 In this example, the classes/com/foo directory contains two classes,
189 com.foo.Hello (the entry point class) and com.foo.NameProvider, both
190 compiled for JDK 8. The classes-10/com/foo directory contains a dif‐
191 ferent version of the com.foo.NameProvider class, this one containing
192 JDK 10 specific code and compiled for JDK 10.
193
194 Given this setup, create a multirelease JAR file foo.jar by running
195 the following command from the directory containing the directories
196 classes and classes-10 .
197
198 jar --create --file foo.jar --main-class com.foo.Hel‐
199 lo -C classes . --release 10 -C classes-10 .
200
201 The JAR file foo.jar now contains:
202
203 % jar -tf foo.jar
204
205 META-INF/
206 META-INF/MANIFEST.MF
207 com/
208 com/foo/
209 com/foo/Hello.class
210 com/foo/NameProvider.class
211 META-INF/versions/10/com/
212 META-INF/versions/10/com/foo/
213 META-INF/versions/10/com/foo/NameProvider.class
214
215 As well as other information, the file META-INF/MANIFEST.MF, will
216 contain the following lines to indicate that this is a multirelease
217 JAR file with an entry point of com.foo.Hello.
218
219 ...
220 Main-Class: com.foo.Hello
221 Multi-Release: true
222
223 Assuming that the com.foo.Hello class calls a method on the
224 com.foo.NameProvider class, running the program using JDK 10 will en‐
225 sure that the com.foo.NameProvider class is the one in META-INF/ver‐
226 sions/10/com/foo/. Running the program using JDK 8 will ensure that
227 the com.foo.NameProvider class is the one at the root of the JAR, in
228 com/foo.
229
230 • Create an archive, my.jar, by reading options and lists of class
231 files from the file classes.list.
232
233 Note:
234
235 To shorten or simplify the jar command, you can specify arguments in
236 a separate text file and pass it to the jar command with the at sign
237 (@) as a prefix.
238
239 jar --create --file my.jar @classes.list
240
241
242
243JDK 17 2021 JAR(1)