How to Use the jar Command - dummies

By Doug Lowe

You use the jar command in Java to create a JAR file, which is a single file that can contain more than one class in a compressed format that the Java Runtime Environment can access quickly. (JAR stands for Java archive.)

A JAR file can have a few or thousands of classes in it. In fact, the entire Java API is stored in a single JAR file named (The rt stands for runtime.) It’s a big file — over 35MB — but that’s not bad considering that it contains more than 12,000 classes.

JAR files are similar in format to Zip files, a compressed format made popular by the PKZIP program. The main difference is that JAR files contain a special file, called the manifest file, which contains information about the files in the archive. This manifest is automatically created by the jar utility, but you can supply a manifest of your own to provide additional information about the archived files.

JAR files are the normal way to distribute finished Java applications. After finishing your application, you run the jar command from a command prompt to prepare the JAR file. Then, another user can copy the JAR file to his or her computer. The user can then run the application directly from the JAR file.

JAR files are also used to distribute class libraries. You can add a JAR file to the ClassPath environment variable. Then, the classes in the JAR file are automatically available to any Java program that imports the package that contains the classes.

The basic format of the jar command is

jar options jar-file [manifest-file] class-files...

The options specify the basic action you want jar to perform and provide additional information about how you want the command to work. Here are the options:

Option Description
c Creates a new jar file.
u Updates an existing jar file.
x Extracts files from an existing jar
t Lists the contents of a jar
f Indicates that the jar file is
specified as an argument. You almost always want to use this
v Verbose output. This option tells the jar command to display extra information while
it works.
0 Doesn’t compress files when it adds them to the archive.
This option isn’t used much.
m Specifies that a manifest file is provided. It’s listed
as the next argument following the jar
M Specifies that a manifest file should not be added to the
archive. This option is rarely used.

Note that you must specify at least the c, u, x, or t option to tell jar what action you want to perform.