jar(1)
π«Ά jar(1) β Basic Tools π«Ά
okay bestie, welcome to jar(1) from Basic Tools β this is the manual page for the jar command, and itβs literally the main-character tool for Java Archive files. π
β¨
π NAME π
jar β manipulates Java Archive (JAR) files. π
β¨ SYNOPSIS β¨
Create JAR file
jar c[efmMnv0] [entrypoint] [jarfile] [manifest] [-C dir] file ... [-Joption ...] [@arg-file ...]
Update JAR file
jar u[efmMnv0] [entrypoint] [jarfile] [manifest] [-C dir] file ... [-Joption ...] [@arg-file ...]
Extract JAR file
jar x[vf] [jarfile] file ... [-Joption ...] [@arg-file ...]
List Contents of JAR file
jar t[vf] [jarfile] file ... [-Joption ...] [@arg-file ...]
Add Index to JAR file
jar i jarfile [-Joption ...] [@arg-file ...]
π¦ DESCRIPTION π¦
so the jar command is a general-purpose archiving and compression tool, based on ZIP and the ZLIB compression format. but like β it was designed mainly to package Java applets or applications into a single archive. π«Ά
when all the components of an applet or application (files, images, and sounds) get combined into one archive, a Java agent (such as a browser) can download them in a single HTTP transaction instead of opening a new connection for every single piece. that dramatically improves download times, no cap. the jar command also compresses files, which further improves download time. π₯
the jar command also lets individual entries in a file be signed by the applet author so their origin can be authenticated. and a JAR file can be used as a class path entry, whether or not it is compressed. π
the syntax for the jar command resembles the syntax for the tar command. it has several operation modes, defined by one of the mandatory operation arguments. the other arguments are either options that modify the behavior of the operation, or operands required to perform the operation. π«
π OPERATION ARGUMENTS π
when you use the jar command, you have to pick an operation by specifying one of these operation arguments. you can mix them up with other one-letter options on the command line, but usually the operation argument is the first argument specified. bestie, choose your fighter: π
cβ create a new JAR archive. πiβ generate index information for a JAR archive. ποΈtβ list the contents of a JAR archive. πuβ update a JAR archive. πxβ extract files from a JAR archive. π¦
π OPTIONS π
use these options to customize how the JAR file is created, updated, extracted, or viewed. every single one matters, okay? π«Ά
eβ sets the class specified by theentrypointoperand to be the entry point for a standalone Java application bundled into an executable JAR file. using this option creates or overrides theMain-Classattribute value in the manifest file. theeoption can be used when creating (c) or updating (u) the JAR file. β¨
for example, this command creates the Main.jar archive with the Main.class file where the Main-Class attribute value in the manifest is set to Main:
jar cfe Main.jar Main Main.class
the Java Runtime Environment (JRE) can directly call this application by running:
java -jar Main.jar
if the entry point class name is in a package, then it could use either the dot (.) or slash (/) as the delimiter. for example, if Main.class is in a package called mydir, then the entry point can be specified in one of these ways:
jar -cfe Main.jar mydir/Main mydir/Main.class
jar -cfe Main.jar mydir.Main mydir/Main.class
β οΈ Note
specifying both m and e options together when a particular manifest also contains the Main-Class attribute results in an ambiguous Main-Class specification. the ambiguity leads to an error and the jar command creation or update operation is terminated. ππ
fβ sets the file specified by thejarfileoperand to be the name of the JAR file that is created (c), updated (u), extracted (x) from, or viewed (t). omitting thefoption and thejarfileoperand instructs thejarcommand to accept the JAR file name from stdin (forxandt) or send the JAR file to stdout (forcandu). πmβ includes names and values of attributes from the file specified by themanifestoperand in the manifest file of thejarcommand (located in the archive atMETA-INF/MANIFEST.MF). thejarcommand adds the attributeβs name and value to the JAR file unless an entry already exists with the same name, in which case thejarcommand updates the value of the attribute. themoption can be used when creating (c) or updating (u) the JAR file. πΈ
you can add special-purpose name-value attribute pairs to the manifest that are not contained in the default manifest file. for example, you can add attributes that specify vendor information, release information, package sealing, or to make JAR-bundled applications executable. for examples of using the m option, see Packaging Programs at http://docs.oracle.com/javase/tutorial/deployment/jar/index.html π¦
Mβ does not create a manifest file entry (forcandu), or delete a manifest file entry when one exists (foru). theMoption can be used when creating (c) or updating (u) the JAR file. π«Άnβ when creating (c) a JAR file, this option normalizes the archive so that the content is not affected by the packing and unpacking operations of thepack200(1)command. without this normalization, the signature of a signed JAR can become invalid. π₯vβ generates verbose output to standard output. see Examples. π0β (Zero) creates (c) or updates (u) the JAR file without using ZIP compression. π-C dirβ when creating (c) or updating (u) a JAR file, this option temporarily changes the directory while processing files specified by thefileoperands. its operation is intended to be similar to the-Coption of the UNIXtarutility. for example, the following command changes to theclassesdirectory and adds theBar.classfile from that directory tomy.jar:
jar uf my.jar -C classes Bar.class
the following command changes to the classes directory and adds to my.jar all files within the classes directory (without creating a classes directory in the JAR file), then changes back to the original directory before changing to the bin directory to add Xyz.class to my.jar:
jar uf my.jar -C classes . -C bin Xyz.class
if classes contained files bar1 and bar2, then the JAR file will contain the following after running the previous command:
% jar tf my.jar
META-INF/
META-INF/MANIFEST.MF
bar1
bar2
Xyz.class
-Joptionβ sets the specified JVM option to be used when the JRE runs the JAR file. JVM options are described on the reference page for thejava(1)command. for example,-J-Xms48msets the startup memory to 48 MB. β
π OPERANDS π
the following operands are recognized by the jar command. π
fileβ when creating (c) or updating (u) a JAR file, thefileoperand defines the path and name of the file or directory that should be added to the archive. when extracting (x) or listing the contents (t) of a JAR file, thefileoperand defines the path and name of the file to be extrated or listed. at least one valid file or directory must be specified. separate multiplefileoperands with spaces. if theentrypoint,jarfile, ormanifestoperands are used, thefileoperands must be specified after them. πentrypointβ when creating (c) or updating (u) a JAR file, theentrypointoperand defines the name of the class that should be the entry point for a standalone Java application bundled into an executable JAR file. theentrypointoperand must be specified if theeoption is present. πjarfileβ defines the name of the file to be created (c), updated (u), extracted (x), or viewed (t). thejarfileoperand must be specified if thefoption is present. omitting thefoption and thejarfileoperand instructs thejarcommand to accept the JAR file name from stdin (forxandt) or send the JAR file to stdout (forcandu). π«Ά
when indexing (i) a JAR file, specify the jarfile operand without the f option. ποΈ
manifestβ when creating (c) or updating (u) a JAR file, themanifestoperand defines the preexisting manifest files with names and values of attributes to be included inMANIFEST.MFin the JAR file. themanifestoperand must be specified if thefoption is present. π@arg-fileβ to shorten or simplify thejarcommand, you can specify arguments in a separate text file and pass it to thejarcommand with the at sign (@) as a prefix. when thejarcommand encounters an argument beginning with the at sign, it expands the contents of that file into the argument list. π§
an argument file can include options and arguments of the jar command (except the -J options, because they are passed to the launcher, which does not support argument files). the arguments within a file can be separated by spaces or newline characters. file names within an argument file are relative to the current directory from which you run the jar command, not relative to the location of the argument file. wild cards, such as the asterisk (*), that might otherwise be expanded by the operating system shell, are not expanded. π
this example shows how to create a classes.list file with names of files from the current directory output by the find command:
find . -name '*.class' -print > classes.list
you can then execute the jar command and pass the classes.list file to it using the @arg-file syntax:
jar cf my.jar @classes.list
an argument file can be specified with a path, but any file names inside the argument file that have relative paths are relative to the current working directory of the jar command, not to the path passed in, for example:
jar @dir/classes.list
π NOTES π
the e, f, and m options must appear in the same order on the command line as the entrypoint, jarfile, and manifest operands, for example:
jar cmef myManifestFile MyMainClass myFile.jar *.class
β¨ EXAMPLES β¨
okay bestie, these examples are iconic and they show the whole jar vibe in action β no cap.
π Example 1 Adding All Files From the Current Directory With Verbose Output π
so you run ls and see the little gang of files chilling there:
% ls
1.au Animator.class monkey.jpg
2.au Wave.class spacemusic.au
3.au at_work.gif
then you hit it with jar cvf and the * wildcard, and boom:
% jar cvf bundle.jar *
added manifest
adding: 1.au(in = 2324) (out= 67)(deflated 97%)
adding: 2.au(in = 6970) (out= 90)(deflated 98%)
adding: 3.au(in = 11616) (out= 108)(deflated 99%)
adding: Animator.class(in = 2266) (out= 66)(deflated 97%)
adding: Wave.class(in = 3778) (out= 81)(deflated 97%)
adding: at_work.gif(in = 6621) (out= 89)(deflated 98%)
adding: monkey.jpg(in = 7667) (out= 91)(deflated 98%)
adding: spacemusic.au(in = 3079) (out= 73)(deflated 97%)
the verbose output is literally telling you every fileβs business π
πΈ Example 2 Adding Files From Subdirectories πΈ
first, peep the directories with ls -F:
% ls -F
audio/ classes/ images/
then add whole directories into the bundle:
% jar cvf bundle.jar audio classes images
added manifest
adding: audio/(in = 0) (out= 0)(stored 0%)
adding: audio/1.au(in = 2324) (out= 67)(deflated 97%)
adding: audio/2.au(in = 6970) (out= 90)(deflated 98%)
adding: audio/3.au(in = 11616) (out= 108)(deflated 99%)
adding: audio/spacemusic.au(in = 3079) (out= 73)(deflated 97%)
adding: classes/(in = 0) (out= 0)(stored 0%)
adding: classes/Animator.class(in = 2266) (out= 66)(deflated 97%)
adding: classes/Wave.class(in = 3778) (out= 81)(deflated 97%)
adding: images/(in = 0) (out= 0)(stored 0%)
adding: images/monkey.jpg(in = 7667) (out= 91)(deflated 98%)
adding: images/at_work.gif(in = 6621) (out= 89)(deflated 98%)
and now bundle.jar is sitting pretty in the directory:
% ls -F
audio/ bundle.jar classes/ images/
ate, no crumbs β¨
π¦ Example 3 Listing the Contents of JAR π¦
wanna see whatβs inside without unpacking? jar tf is your bestie:
% jar tf bundle.jar
META-INF/
META-INF/MANIFEST.MF
audio/1.au
audio/2.au
audio/3.au
audio/spacemusic.au
classes/Animator.class
classes/Wave.class
images/monkey.jpg
images/at_work.gif
look at that manifest and all those files just vibing in a neat little list π«Ά
π Example 4 Adding an Index π
Use the i option when you split the interdependent classes for a stock trade application into three JAR files: main.jar, buy.jar, and sell.jar. If you specify the Class-Path attribute in the main.jar manifest, then you can use the i option to speed up the class loading time for your application:
Class-Path: buy.jar sell.jar
jar i main.jar
An INDEX.LIST file is inserted to the META-INF directory. This enables the application class loader to download the specified JAR files when it is searching for classes or resources.
The application class loader uses the information stored in this file for efficient class loading. To copy directories, first compress files in dir1 to stdout, then pipeline and extract from stdin to dir2 (omitting the -f option from both jar commands):
(cd dir1; jar c .) | (cd dir2; jar x)
that pipeline is giving main character energy, fr fr π₯
π SEE ALSO π
pack200(1).- The JAR section of The Java Tutorials at
http://docs.oracle.com/javase/tutorial/deployment/jar/index.html
and thatβs the whole page, bestie β this iconic jar(1) moment comes from JDK 8, dated 21 November 2013. slay forever π
β¨
π¦ where this page comes from
- arch rolling: jdk8-openjdk 8.504.u01-1 Β· LicenseRef-Java
- Copyright (c) 1997, 2013, 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