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
147 --date=TIMESTAMP
148 The timestamp in ISO-8601 extended offset date-time with option‐
149 al time-zone format, to use for the timestamp of the entries,
150 e.g. "2022-02-12T12:30:00-05:00".
151
153 The following options are recognized by the jar command and not used
154 with operation modes:
155
156 -h or --help[:compat]
157 Displays the command-line help for the jar command or optionally
158 the compatibility help.
159
160 --help-extra
161 Displays help on extra options.
162
163 --version
164 Prints the program version.
165
167 • Create an archive, classes.jar, that contains two class files,
168 Foo.class and Bar.class.
169
170 jar --create --file classes.jar Foo.class Bar.class
171
172 • Create an archive, classes.jar, that contains two class files,
173 Foo.class and Bar.class setting the last modified date and time to
174 2021 Jan 6 12:36:00.
175
176 jar --create --date="2021-01-06T14:36:00+02:00" --file=class‐
177 es.jar Foo.class Bar.class
178
179 • Create an archive, classes.jar, by using an existing manifest, myman‐
180 ifest, that contains all of the files in the directory foo/.
181
182 jar --create --file classes.jar --manifest mymanifest -C foo/
183
184 • Create a modular JAR archive,foo.jar, where the module descriptor is
185 located in classes/module-info.class.
186
187 jar --create --file foo.jar --main-class com.foo.Main --mod‐
188 ule-version 1.0 -C foo/classes resources
189
190 • Update an existing non-modular JAR, foo.jar, to a modular JAR file.
191
192 jar --update --file foo.jar --main-class com.foo.Main --mod‐
193 ule-version 1.0 -C foo/module-info.class
194
195 • Create a versioned or multi-release JAR, foo.jar, that places the
196 files in the classes directory at the root of the JAR, and the files
197 in the classes-10 directory in the META-INF/versions/10 directory of
198 the JAR.
199
200 In this example, the classes/com/foo directory contains two classes,
201 com.foo.Hello (the entry point class) and com.foo.NameProvider, both
202 compiled for JDK 8. The classes-10/com/foo directory contains a dif‐
203 ferent version of the com.foo.NameProvider class, this one containing
204 JDK 10 specific code and compiled for JDK 10.
205
206 Given this setup, create a multirelease JAR file foo.jar by running
207 the following command from the directory containing the directories
208 classes and classes-10 .
209
210 jar --create --file foo.jar --main-class com.foo.Hel‐
211 lo -C classes . --release 10 -C classes-10 .
212
213 The JAR file foo.jar now contains:
214
215 % jar -tf foo.jar
216
217 META-INF/
218 META-INF/MANIFEST.MF
219 com/
220 com/foo/
221 com/foo/Hello.class
222 com/foo/NameProvider.class
223 META-INF/versions/10/com/
224 META-INF/versions/10/com/foo/
225 META-INF/versions/10/com/foo/NameProvider.class
226
227 As well as other information, the file META-INF/MANIFEST.MF, will
228 contain the following lines to indicate that this is a multirelease
229 JAR file with an entry point of com.foo.Hello.
230
231 ...
232 Main-Class: com.foo.Hello
233 Multi-Release: true
234
235 Assuming that the com.foo.Hello class calls a method on the
236 com.foo.NameProvider class, running the program using JDK 10 will en‐
237 sure that the com.foo.NameProvider class is the one in META-INF/ver‐
238 sions/10/com/foo/. Running the program using JDK 8 will ensure that
239 the com.foo.NameProvider class is the one at the root of the JAR, in
240 com/foo.
241
242 • Create an archive, my.jar, by reading options and lists of class
243 files from the file classes.list.
244
245 Note:
246
247 To shorten or simplify the jar command, you can specify arguments in
248 a separate text file and pass it to the jar command with the at sign
249 (@) as a prefix.
250
251 jar --create --file my.jar @classes.list
252
253
254
255JDK 19 2022 JAR(1)