DevOps Essentials

View on GitHub



jar - Java archive tool


jar c[v0M]f jarfile [ -C dir ] inputfiles [ -Joption ]

jar c[v0]mf manifest jarfile [ -C dir ] inputfiles [ -Joption ]

jar c[v0M] [ -C dir ] inputfiles [ -Joption ]

jar c[v0]m manifest [ -C dir ] inputfiles [ -Joption ]

jar u[v0M]f jarfile [ -C dir ] inputfiles [ -Joption ]

jar u[v0]mf manifest jarfile [ -C dir ] inputfiles [ -Joption ]

jar u[v0M] [ -C dir ] inputfiles [ -Joption ]

jar u[v0]m manifest [ -C dir ] inputfiles [ -Joption ]

jar x[v]f jarfile [ inputfiles ] [ -Joption ]

jar x[v] [ inputfiles ] [ -Joption ]

jar t[v] [ inputfiles ] [ -Joption ]

Add index to jar file

jar i jarfile [ -Joption ]



The jar tool combines multiple files into a single JAR archive file.

jar is a general-purpose archiving and compression tool, based on ZIP and the ZLIB compression format.


Typical usage to combine files into a jar file is:

$ jar cf myFile.jar *.class

In this example, all the class files in the current directory are placed in the file named myjarfile. A manifest file entry named META-INF/MANIFEST.MF is automatically generated by the jar tool and is always the first entry in the jar file. The manifest file is the place where any meta-information about the archive is stored as name:value pairs. Refer to the Jar File specification for details about how meta-information is stored in the manifest file.

If you have a pre-existing manifest file whose name: value pairs you want the jar tool to include for the new jar archive, you can specify it using the m option:

$ jar cmf myManifestFile myJarFile *.class

To make jar archive of foo.class with the name as foo.jar

$ jar uf foo.jar foo.class

The following would add the file foo.class to the existing jar file foo.jar. The u option can also update the manifest entry, as given by following example which updates the foo.jar manifest with the name: value pairs in manifest.

$ jar umf manifest foo.jar

To see the contents of an ear file, you can similarly use

$ jar -tvf ns-server-ear-xx- | grep ns-xx-model-
1390355 Thu Feb 07 11:53:32 +07 2019 lib/ns-xx-model-

To update this with new jar file we can use following

Will replace the jar file lib/ns-ghoperations-model- in the ear ns-server-ear-tg- assuming that it is in the directory lib/ in the ear

$ jar -uvf ns-server-ear-xx- lib/ns-xx-model-
updating: ns-xx-model- (in=1390356) (out=1351630) (deflated 2%)
(in = 62514279) (out = 57862898) (deflated 7%)

$ jar -tvf ns-server-ear-tg- | grep ns-ghoperations-model-
1390356 Thu Feb 28 01:25:04 +07 2019 lib/ns-ghoperations-model-

To extract the files from a jar file, use x , as in:

$ jar xf myFile.jar

Following would change to the classes directory and add the bar.class from that directory to foo.jar.

$ jar uf foo.jar -C classes bar.classes

The following command, would change to the classes directory and add to foo.jar all files within the classes directory (without creating a classes directory in the jar file), then change back to the original directory before changing to the bin directory to add xyz.class to foo.jar. If classes holds files bar1 and bar2, then here’s what the jar file would contain using jar tf foo.jar:

$ jar uf foo.jar -C classes . -C bin xyz.class