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. This
67 option is deprecated and may be removed in a future release.
68
69 -t or --list
70 Lists the table of contents for the archive.
71
72 -u or --update
73 Updates an existing JAR file.
74
75 -x or --extract
76 Extracts the named (or all) files from the archive.
77
78 -d or --describe-module
79 Prints the module descriptor or automatic module name.
80
82 You can use the following options to customize the actions of any oper‐
83 ation mode included in the jar command.
84
85 -C DIR Changes the specified directory and includes the files specified
86 at the end of the command line.
87
88 jar [OPTION ...] [ [--release VERSION] [-C dir] files]
89
90 -f FILE or --file=FILE
91 Specifies the archive file name.
92
93 --release VERSION
94 Creates a multirelease JAR file. Places all files specified af‐
95 ter the option into a versioned directory of the JAR file named
96 META-INF/versions/VERSION/, where VERSION must be must be a pos‐
97 itive integer whose value is 9 or greater.
98
99 At run time, where more than one version of a class exists in
100 the JAR, the JDK will use the first one it finds, searching ini‐
101 tially in the directory tree whose VERSION number matches the
102 JDK's major version number. It will then look in directories
103 with successively lower VERSION numbers, and finally look in the
104 root of the JAR.
105
106 -v or --verbose
107 Sends or prints verbose output to standard output.
108
110 You can use the following options to customize the actions of the cre‐
111 ate and the update main operation modes:
112
113 -e CLASSNAME or --main-class=CLASSNAME
114 Specifies the application entry point for standalone applica‐
115 tions bundled into a modular or executable modular JAR file.
116
117 -m FILE or --manifest=FILE
118 Includes the manifest information from the given manifest file.
119
120 -M or --no-manifest
121 Doesn't create a manifest file for the entries.
122
123 --module-version=VERSION
124 Specifies the module version, when creating or updating a modu‐
125 lar JAR file, or updating a non-modular JAR file.
126
127 --hash-modules=PATTERN
128 Computes and records the hashes of modules matched by the given
129 pattern and that depend upon directly or indirectly on a modular
130 JAR file being created or a non-modular JAR file being updated.
131
132 -p or --module-path
133 Specifies the location of module dependence for generating the
134 hash.
135
136 @file Reads jar options and file names from a text file.
137
139 You can use the following options to customize the actions of the cre‐
140 ate (-c or --create) the update (-u or --update ) and the generate-in‐
141 dex (-i or --generate-index=FILE) main operation modes:
142
143 -0 or --no-compress
144 Stores without using ZIP compression.
145
146 --date=TIMESTAMP
147 The timestamp in ISO-8601 extended offset date-time with option‐
148 al time-zone format, to use for the timestamp of the entries,
149 e.g. "2022-02-12T12:30:00-05:00".
150
152 The following options are recognized by the jar command and not used
153 with operation modes:
154
155 -h or --help[:compat]
156 Displays the command-line help for the jar command or optionally
157 the compatibility help.
158
159 --help-extra
160 Displays help on extra options.
161
162 --version
163 Prints the program version.
164
166 • Create an archive, classes.jar, that contains two class files,
167 Foo.class and Bar.class.
168
169 jar --create --file classes.jar Foo.class Bar.class
170
171 • Create an archive, classes.jar, that contains two class files,
172 Foo.class and Bar.class setting the last modified date and time to
173 2021 Jan 6 12:36:00.
174
175 jar --create --date="2021-01-06T14:36:00+02:00" --file=class‐
176 es.jar Foo.class Bar.class
177
178 • Create an archive, classes.jar, by using an existing manifest, myman‐
179 ifest, that contains all of the files in the directory foo/.
180
181 jar --create --file classes.jar --manifest mymanifest -C foo/
182
183 • Create a modular JAR archive,foo.jar, where the module descriptor is
184 located in classes/module-info.class.
185
186 jar --create --file foo.jar --main-class com.foo.Main --mod‐
187 ule-version 1.0 -C foo/classes resources
188
189 • Update an existing non-modular JAR, foo.jar, to a modular JAR file.
190
191 jar --update --file foo.jar --main-class com.foo.Main --mod‐
192 ule-version 1.0 -C foo/module-info.class
193
194 • Create a versioned or multi-release JAR, foo.jar, that places the
195 files in the classes directory at the root of the JAR, and the files
196 in the classes-10 directory in the META-INF/versions/10 directory of
197 the JAR.
198
199 In this example, the classes/com/foo directory contains two classes,
200 com.foo.Hello (the entry point class) and com.foo.NameProvider, both
201 compiled for JDK 8. The classes-10/com/foo directory contains a dif‐
202 ferent version of the com.foo.NameProvider class, this one containing
203 JDK 10 specific code and compiled for JDK 10.
204
205 Given this setup, create a multirelease JAR file foo.jar by running
206 the following command from the directory containing the directories
207 classes and classes-10 .
208
209 jar --create --file foo.jar --main-class com.foo.Hello -C
210 classes . --release 10 -C classes-10 .
211
212 The JAR file foo.jar now contains:
213
214 % jar -tf foo.jar
215
216 META-INF/
217 META-INF/MANIFEST.MF
218 com/
219 com/foo/
220 com/foo/Hello.class
221 com/foo/NameProvider.class
222 META-INF/versions/10/com/
223 META-INF/versions/10/com/foo/
224 META-INF/versions/10/com/foo/NameProvider.class
225
226 As well as other information, the file META-INF/MANIFEST.MF, will
227 contain the following lines to indicate that this is a multirelease
228 JAR file with an entry point of com.foo.Hello.
229
230 ...
231 Main-Class: com.foo.Hello
232 Multi-Release: true
233
234 Assuming that the com.foo.Hello class calls a method on the
235 com.foo.NameProvider class, running the program using JDK 10 will en‐
236 sure that the com.foo.NameProvider class is the one in META-INF/ver‐
237 sions/10/com/foo/. Running the program using JDK 8 will ensure that
238 the com.foo.NameProvider class is the one at the root of the JAR, in
239 com/foo.
240
241 • Create an archive, my.jar, by reading options and lists of class
242 files from the file classes.list.
243
244 Note:
245
246 To shorten or simplify the jar command, you can specify arguments in
247 a separate text file and pass it to the jar command with the at sign
248 (@) as a prefix.
249
250 jar --create --file my.jar @classes.list
251
252
253
254JDK 21 2023 JAR(1)