1jar(1) General Commands Manual jar(1)
2
3
4
6 jar-The Java Archive Tool
7
8 jar combines multiple files into a single JAR archive file.
9
11 Create jar file
12 jar c[v0Mmfe] [manifest] [jarfile] [entrypoint] [-C dir] input‐
13 files [-Joption]
14
15 Update jar file
16 jar u[v0Mmfe] [manifest] [jarfile] [entrypoint] [-C dir] input‐
17 files [-Joption]
18
19 Extract jar file
20 jar x[vf] [jarfile] [inputfiles] [-Joption]
21
22 List table of contents of jar file
23 jar t[vf] [jarfile] [inputfiles] [-Joption]
24
25 Add index to jar file
26 jar i jarfile [-Joption]
27
28
29 where:
30
31 cuxtiv0Mmfe
32 Options that control the jar command.
33
34 jarfile
35 Jar file to be created (c), updated (u), extracted (x), or have
36 its table of contents viewed (t). The -f option and filename
37 jarfile are a pair -- if either is present, they must both
38 appear. Note that omitting f and jarfile accepts a "jar file"
39 from standard input (for x and t) or sends the "jar file" to
40 standard output (for c and u).
41
42 inputfiles
43 Files or directories, separated by spaces, to be combined into
44 jarfile (for c and u), or to be extracted (for x) or listed (for
45 t) from jarfile. All directories are processed recursively. The
46 files are compressed unless option 0 (zero) is used.
47
48 manifest
49 Pre-existing manifest file whose name: value pairs are to be
50 included in MANIFEST.MF in the jar file. The -m option and file‐
51 name manifest are a pair -- if either is present, they must both
52 appear. The letters m, f and e must appear in the same order that
53 manifest, jarfile, entrypoint appear.
54
55 entrypoint
56 The name of the class that set as the application entry point for
57 stand-alone applications bundled into executable jar file. The -e
58 option and entrypoint are a pair -- if either is present, they
59 must both appear. The letters m, f and e must appear in the same
60 order that manifest, jarfile, entrypoint appear.
61
62 -C dir
63 Temporarily changes directories to dir while processing the fol‐
64 lowing inputfiles argument. Multiple -C dir inputfiles sets are
65 allowed.
66
67 -Joption
68 Option to be passed into the Java runtime environment. (There
69 must be no space between -J and option).
70
71
73 The jar tool combines multiple files into a single JAR archive file.
74 jar is a general-purpose archiving and compression tool, based on ZIP
75 and the ZLIB @
76 http://www.gzip.org/zlib/ compression format. However, jar was designed
77 mainly package java applets or applications into a single archive. When
78 the components of an applet or application (files, images and sounds)
79 are combined into a single archive, they can be downloaded by a java
80 agent (like a browser) in a single HTTP transaction, rather than
81 requiring a new connection for each piece. This dramatically improves
82 download times. jar also compresses files and so further improves down‐
83 load time. In addition, it allows individual entries in a file to be
84 signed by the applet author so that their origin can be authenticated.
85 The syntax for the jar tool is almost identical to the syntax for the
86 tar command. A jar archive can be used as a class path entry, whether
87 or not it is compressed.
88
89 Typical usage to combine files into a jar file is:
90
91 % jar cf myFile.jar *.class
92
93
94 In this example, all the class files in the current directory are
95 placed into the file named myFile.jar. The jar tool automatically gen‐
96 erates a manifest file entry named META-INF/MANIFEST.MF. It is always
97 the first entry in the jar file. The manifest file declares meta-infor‐
98 mation about the archive, and stores that data as name : value pairs.
99 Refer to the JAR file specification @
100 http://docs.oracle.com/javase/7/docs/tech‐
101 notes/guides/jar/jar.html#JAR%20Manifest for details explaining how the
102 jar tool stores meta-information in the manifest file.
103
104 If a jar file should include name : value pairs contained in an exist‐
105 ing manifest file, specify that file using the -m option:
106
107 % jar cmf myManifestFile myFile.jar *.class
108
109
110 An existing manifest file must end with a new line character. jar does
111 not parse the last line of a manifest file if it does not end with a
112 new line character.
113
114
115 Note: A jar command that specifies cfm on the command line instead of
116 cmf (the order of the m and -f options are reversed), the jar command
117 line must specify the name of the jar archive first, followed by the
118 name of the manifest file:
119 % jar cfm myFile.jar myManifestFile *.class
120
121
122 The manifest is in a text format inspired by RFC822 ASCII format, so it
123 is easy to view and process manifest-file contents.
124
125 To extract the files from a jar file, use x:
126
127 % jar xf myFile.jar
128
129
130 To extract individual files from a jar file, supply their filenames:
131
132 % jar xf myFile.jar foo bar
133
134
135 Beginning with version 1.3 of the JDK, the jar utility supports
136 JarIndex @
137 http://docs.oracle.com/javase/7/docs/tech‐
138 notes/guides/jar/jar.html#JAR_Index, which allows application class
139 loaders to load classes more efficiently from jar files. If an applica‐
140 tion or applet is bundled into multiple jar files, only the necessary
141 jar files will be downloaded and opened to load classes. This perfor‐
142 mance optimization is enabled by running jar with the -ioption. It will
143 generate package location information for the specified main jar file
144 and all the jar files it depends on, which need to be specified in the
145 Class-Path attribute of the main jar file's manifest.
146
147 % jar i main.jar
148
149
150 In this example, an INDEX.LIST file is inserted into the META-INF
151 directory of main.jar.
152 The application class loader uses the information stored in this file
153 for efficient class loading. For details about how location informa‐
154 tion is stored in the index file, refer to the JarIndex specification.
155 To copy directories, first compress files in dir1 to stdout, then
156 extract from stdin to dir2 (omitting the -f option from both jar com‐
157 mands):
158
159 % (cd dir1; jar c .) | (cd dir2; jar x)
160
161
162 To review command samples which use jar to opeate on jar files and jar
163 file manifests, see Examples, below. Also refer to the jar trail of the
164 Java Tutorial @
165 http://docs.oracle.com/javase/tutorial/deployment/jar.
166
168 c Creates a new archive file named jarfile (if f is specified) or
169 to standard output (if f and jarfile are omitted). Add to it the
170 files and directories specified by inputfiles.
171
172 u Updates an existing file jarfile (when f is specified) by adding
173 to it files and directories specified by inputfiles. For example:
174 jar uf foo.jar foo.class
175 would add the file foo.class to the existing jar file foo.jar.
176 The -u option can also update the manifest entry, as given by
177 this example:
178 jar umf manifest foo.jar
179 updates the foo.jar manifest with the name : value pairs in mani‐
180 fest.
181
182 x Extracts files and directories from jarfile (if f is specified)
183 or standard input (if f and jarfile are omitted). If inputfiles
184 is specified, only those specified files and directories are
185 extracted. Otherwise, all files and directories are extracted.
186 The time and date of the extracted files are those given in the
187 archive.
188
189 t Lists the table of contents from jarfile (if f is specified) or
190 standard input (if f and jarfile are omitted). If inputfiles is
191 specified, only those specified files and directories are listed.
192 Otherwise, all files and directories are listed.
193
194 i Generate index information for the specified jarfile and its
195 dependent jar files. For example:
196 jar i foo.jar
197
198 would generate an INDEX.LIST file in foo.jar which contains location
199 information for each package in foo.jar and all the jar files speci‐
200 fied in the Class-Path attribute of foo.jar. See the index example.
201
202 f Specifies the file jarfile to be created (c), updated (u),
203 extracted (x), indexed (i), or viewed (t). The -f option and
204 filename jarfile are a pair -- if present, they must both appear.
205 Omitting f and jarfile accepts a jar file name from stdin(for x
206 and t) or sends jar file to stdout (for c and u).
207
208 v Generates verbose output to standard output. Examples shown
209 below.
210
211 0 (zero) Store without using ZIP compression.
212
213 M Do not create a manifest file entry (for c and u), or delete a
214 manifest file entry if one exists (for u).
215
216 m Includes name : value attribute pairs from the specified manifest
217 file manifest in the file at META-INF/MANIFEST.MF. jar adds a
218 name : value pair unless an entry already exists with the same
219 name, in which case jar updates its value.
220 On the command line, the letters m and f must appear in the same
221 order that manifest and jarfile appear. Example use:
222 jar cmf myManifestFile myFile.jar *.class
223 You can add special-purpose name : value attribute pairs to the
224 manifest that aren't contained in the default manifest. For exam‐
225 ple, you can add attributes specifying vendor information, ver‐
226 sion information, package sealing, or to make JAR-bundled appli‐
227 cations executable. See the JAR Files @
228 http://docs.oracle.com/javase/tutorial/deployment/jar/ trail in
229 the Java Tutorial for examples of using the -m option.
230
231 e Sets entrypoint as the application entry point for stand-alone
232 applications bundled into executable jar file. The use of this
233 option creates or overrides the Main-Class attribute value in the
234 manifest file. This option can be used during creation of jar
235 file or while updating the jar file. This option specifies the
236 application entry point without editing or creating the manifest
237 file.
238 For example, this command creates Main.jar where the Main-Class
239 attribute value in the manifest is set to Main:
240 jar cfe Main.jar Main Main.class
241 The java runtime can directly invoke this application by running
242 the following command:
243 java -jar Main.jar
244 If the entrypoint class name is in a package it may use either a
245 dot (".") or slash ("/") character as the delimiter. For example,
246 if Main.class is in a package called foo the entry point can be
247 specified in the following ways:
248 jar -cfe Main.jar foo/Main foo/Main.class
249 or
250 jar -cfe Main.jar foo.Main foo/Main.class
251 Note: specifying both -m and -e options together when the given
252 manifest also contains the Main-Class attribute results in an
253 ambigous Main.class specification, leading to an error and the
254 jar creation or update operation is aborted.
255
256 -C dir
257 Temporarily changes directories (cd dir) during execution of the
258 jar command while processing the following inputfiles argument.
259 Its operation is intended to be similar to the -C option of the
260 UNIX tar utility.
261 For example, this command changes to the classes directory and
262 adds the bar.class from that directory to foo.jar:
263 jar uf foo.jar -C classes bar.class
264 This command changes to the classes directory and adds to foo.jar
265 all files within the classes directory (without creating a
266 classes directory in the jar file), then changes back to the
267 original directory before changing to the bin directory to add
268 xyz.class to foo.jar.
269 jar uf foo.jar -C classes . -C bin xyz.class
270 If classes holds files bar1 and bar2, then here's what the jar
271 file will contain using jar tf foo.jar:
272 META-INF/
273 META-INF/MANIFEST.MF
274 bar1
275 bar2
276 xyz.class
277
278 -Joption
279 Pass option to the Java runtime environment, where option is one
280 of the options described on the reference page for the java
281 application launcher. For example, -J-Xmx48M sets the maximum
282 memory to 48 megabytes. It is a common convention for -J to pass
283 options to the underlying runtime environment.
284
285
287 To shorten or simplify the jar command line, you can specify one or
288 more files that themselves contain arguments to the jar command (except
289 -J options). This enables you to create jar commands of any length,
290 overcoming command line limits imposed by the operating system.
291
292 An argument file can include options and filenames. The arguments
293 within a file can be space-separated or newline-separated. Filenames
294 within an argument file are relative to the current directory, not rel‐
295 ative to the location of the argument file. Wildcards (*) that might
296 otherwise be expanded by the operating system shell are not expanded.
297 Use of the @ character to recursively interpret files is not supported.
298 The -J options are not supported because they are passed to the
299 launcher, which does not support argument files.
300
301 When executing jar, pass in the path and name of each argument file
302 with the @ leading character. When jar encounters an argument beginning
303 with the character @, it expands the contents of that file into the
304 argument list.
305 The example below, classes.list holds the names of files output by a
306 find command:
307
308 % find . -name '*.class' -print > classes.list
309
310
311 You can then execute the jar command on Classes.list by passing it to
312 jar using argfile syntax:
313
314 % jar cf my.jar @classes.list
315
316
317 An argument file can specify a path, but any filenames inside the argu‐
318 ment file that have relative paths are relative to the current working
319 directory, not to the path passed in. Here is an example:
320 % jar @path1/classes.list
321
322
324 To add all the files in a particular directory to an archive (overwrit‐
325 ing contents if the archive already exists). Enumerating verbosely
326 (with the -v option) will tell you more information about the files in
327 the archive, such as their size and last modified date.
328 % ls
329 1.au Animator.class monkey.jpg
330 2.au Wave.class spacemusic.au
331 3.au at_work.gif
332
333 % jar cvf bundle.jar *
334 added manifest
335 adding: 1.au(in = 2324) (out= 67)(deflated 97%)
336 adding: 2.au(in = 6970) (out= 90)(deflated 98%)
337 adding: 3.au(in = 11616) (out= 108)(deflated 99%)
338 adding: Animator.class(in = 2266) (out= 66)(deflated 97%)
339 adding: Wave.class(in = 3778) (out= 81)(deflated 97%)
340 adding: at_work.gif(in = 6621) (out= 89)(deflated 98%)
341 adding: monkey.jpg(in = 7667) (out= 91)(deflated 98%)
342 adding: spacemusic.au(in = 3079) (out= 73)(deflated 97%)
343
344
345 If you already have separate subdirectories for images, audio files and
346 classes, you can combine them into a single jar file:
347 % ls -F
348 audio/ classes/ images/
349
350 % jar cvf bundle.jar audio classes images
351 added manifest
352 adding: audio/(in = 0) (out= 0)(stored 0%)
353 adding: audio/1.au(in = 2324) (out= 67)(deflated 97%)
354 adding: audio/2.au(in = 6970) (out= 90)(deflated 98%)
355 adding: audio/3.au(in = 11616) (out= 108)(deflated 99%)
356 adding: audio/spacemusic.au(in = 3079) (out= 73)(deflated 97%)
357 adding: classes/(in = 0) (out= 0)(stored 0%)
358 adding: classes/Animator.class(in = 2266) (out= 66)(deflated 97%)
359 adding: classes/Wave.class(in = 3778) (out= 81)(deflated 97%)
360 adding: images/(in = 0) (out= 0)(stored 0%)
361 adding: images/monkey.jpg(in = 7667) (out= 91)(deflated 98%)
362 adding: images/at_work.gif(in = 6621) (out= 89)(deflated 98%)
363
364 % ls -F
365 audio/ bundle.jar classes/ images/
366
367
368 To see the entry names in the jarfile, use the t option:
369 % jar tf bundle.jar
370 META-INF/
371 META-INF/MANIFEST.MF
372 audio/1.au
373 audio/2.au
374 audio/3.au
375 audio/spacemusic.au
376 classes/Animator.class
377 classes/Wave.class
378 images/monkey.jpg
379 images/at_work.gif
380
381
382 To add an index file to the jar file for speeding up class loading, use
383 the i option.
384 Example:
385
386
387 If you split the inter-dependent classes for a stock trade application
388 into three jar files: main.jar, buy.jar, and sell.jar.
389
390
391 If you specify the Class-path attribute in the main.jar manifest as:
392 Class-Path: buy.jar sell.jar
393
394
395 then you can use the -i option to speed up the class loading time for
396 your application:
397 % jar i main.jar
398
399
400 An INDEX.LIST file is inserted to the META-INF directory. This enables
401 the application class loader to download the specified jar files when
402 it is searching for classes or resources.
403
405 The Jar Overview @
406 http://docs.oracle.com/javase/7/docs/technotes/guides/jar/jarGuide.html
407
408 The Jar File Specification @
409 http://docs.oracle.com/javase/7/docs/technotes/guides/jar/jar.html
410
411 The JarIndex Spec @
412 http://docs.oracle.com/javase/7/docs/tech‐
413 notes/guides/jar/jar.html#JAR_Index
414
415 Jar Tutorial @
416 http://docs.oracle.com/javase/tutorial/deployment/jar/index.html
417
418 pack200(1)
419
420 16 Mar 2012 jar(1)