The JDK provides the Java Archive Tool to perform basic operations with JAR files such as creating, viewing, updating, and extracting. You can invoke this tool using the jar command.
Creating a JAR
You can create a JAR file using the jar tool. In this topic, we'll give you just the basic understanding of how to use the jar tool. In practice, you should use an IDE or a build tool to create a JAR file instead. However, knowing how to use the jar tool will help you better understand how more advanced tools do the same thing.
To create a Java archive, use the following command:
jar cvf jar-file input-file(s)The options and arguments:
the
coption indicates that you want to create a new JAR file;the
voption asks for verbose mode, which displays information messages about adding each file to the JAR;the
foption specifies that the output goes to a file specified injar-file(rather than the standard output by default).the
jar-fileargument is a name that you want the resulting JAR file to have.the
input-filesargument is a space-separated list of files or directories for grouping them together in the resulting JAR file. The argument can contain the wildcard*symbol. The contents of directories are added to the JAR recursively.
Suppose the current directory has two .class files and a subdirectory called images containing photos of animals. The simplest way to create a JAR file is to enumerate all the input files.
jar cvf application.jar Main.class Animal.class imagesThe output would be the following (because we used the v option):
added manifest
adding: Main.class(in = 282) (out= 192)(deflated 31%)
adding: Animal.class(in = 425) (out= 291)(deflated 31%)
adding: images/(in = 0) (out= 0)(stored 0%)
adding: images/cat.png(in = 1043711) (out= 1034491)(deflated 0%)
adding: images/dog.png(in = 16819) (out= 16784)(deflated 0%)The deflated value is a compression rate for each included file.
The result is a file named application.jar with the following structure:
application.jar
├── META-INF
│ └── MANIFEST.MF
├── images
│ ├── cat.png
│ └── dog.png
├── Animal.class
└── Main.classThis JAR has a default manifest file that does not contain information about the location of the main method. If you want to start this program, specify a class containing the main method using the classpath (-cp):
java -cp application.jar Main
Please note, that we use the default package for .class files, but it is better not to use it in practice.
If the current directory does not contain unnecessary .class files, just use the wildcard * symbol to put all the suitable files into the JAR:
jar cvf application.jar *.class imagesThe resulting JAR has the same structure as above.
Creating an executable JAR
Executable JAR should have the Main-Class attribute in the manifest file. It is possible to provide an existing manifest when creating a JAR file.
We need to prepare a text file (to use it as input manifest) called manifest.mf. This file specifies a class that contains the entry main method for launching the application. Note that the file MUST BE terminated with a blank line as shown below.
Manifest-Version: 1.0
Main-Class: Main
Next, create the JAR file with the input manifest using the command-line jar tool:
jar cvfm application.jar manifest.mf *.class images The option m specifies the inclusion of an input manifest.
To run this application, type the following command:
java -jar application.jarViewing and extracting content
To view individual files within a JAR file and/or extract its content, you can use something like 7-Zip, WinRAR, or a file manager like Midnight Manager. The jar tool also makes it possible to view and extract the contents of a JAR. We will demonstrate this using the application.jar created before.
To see or unpack the content of a JAR you may open it as a regular ZIP archive or use the command-line jar tool.
To view the content of this JAR file do the following:
jar tf application.jarThis command outputs:
META-INF/
META-INF/MANIFEST.MF
Animal.class
Main.class
images/
images/cat.png
images/dog.pngThe t option (table) indicates that you want to view the table of contents, and the f option (file) specifies the name of a JAR file.
To extract the content of this JAR file you should use this:
jar xf application.jarTo prevent overwriting of existing files, you can use the
koption during extraction:
jar xkf application.jarTo extract a JAR file directly to a specified directory, for example:
/tmp/output, you can use the-Coption:
jar -xf application.jar -C /tmp/output/The x option indicates that you want to extract files from the JAR archive, and the f option (file) specifies the name of a JAR file.
Updating a JAR
You can update the contents of an existing JAR file by modifying its manifest or by adding files.
To update a JAR file using the jar tool, type the following command:
jar uf jar-file input-file(s)The options and arguments are:
the
uoption indicates that you want to update an existing JAR;the
foption indicates that the JAR file to update is specified as the argument;the
jar-fileargument is the JAR file to be updated;the
input-filesargument is a space-separated list of files that you want to add to the JAR.
This command overrides existing files with the same full name as the added files.
Let's add two files to the application.jar.
jar uf application.jar Zoo.class images/zoo.pngNow the archive includes the Zoo.class in the same place, where Main.class and Animal.class are located (see the hierarchy above). The zoo.png file has been added to the images directory within the JAR.
With the u option we can also update the existing manifest to add the Main-Class attribute.
Signing and Verifying JAR Files
In addition to creating, updating, and extracting JAR files, you can digitally sign a JAR file to verify its authenticity and integrity. Java provides the jarsigner tool to sign JAR files with a private key and to verify their signatures using the corresponding public certificate. When you distribute a JAR file — especially in contexts where security is important, like applets, web-start applications, or plugins it is important to sign a jar file to:
Prove the identity of the author
Ensure that the file has not been modified since it was signed
To verify the signature of a JAR, you can use the following command:
jarsigner -verify application.jarThis command checks the signatures and ensures that the JAR has not been tampered with.
You can also use the -verbose option for detailed output:
jarsigner -verify -verbose application.jarIf you are using JDK 21 or higher, the jarsigner -verify command has been enhanced to detect cases where a file that was originally signed has been removed from the JAR. Previously, the verification API could not distinguish between a file that was never present and a file that was deleted after signing. Now, when verifying a signed JAR:
If the signature file references an entry that no longer exists in the archive,
jarsignerwill print a warning:This JAR contains signed entries for files that do not existTo see which files are missing, add the
-verboseflag to the verify command:jarsigner -verify -verbose application.jar
Conclusion
Using the simple jar tool, you can easily manage small JAR files. However, it can be quite complicated in practice, since real projects may contain hundreds and thousands of files located in many packages. So, it is better to use a build tool like Maven or Gradle. In any case, knowing about this tool will help you to understand how build tools actually work and why it is necessary to use them.
You can read about the jar tool here.