Java Reference
In-Depth Information
Table 9-3
(
continued
)
Option
Description
-M
Do not create a manifest file.
-i
Generate index information for the specified JAR file. It creates an INDEX.LIST file in JAR file under the
META-INF directory.
-0
Do not compress the entries in the JAR file. Only store them. The option value is zero, which means
zero compression.
-e
Add the specified class name as the value for the Main-Class entry in the main section of the
manifest file.
-v
Generate verbose output on the standard output
-C
Change to the specified directory and include the following files in a JAR file. Note that the option is in
uppercase (C). The lowercase (c) is used to indicate the create JAR file option.
Creating a JAR File
Use the following command to create a
test.jar
JAR file with two class files called
A.class
and
B.class
:
jar cf test.jar A.class B.class
If you get an error such as “jar is not recognized as a command” when you run this command, you need to use
the full path of the
jar
command or add the directory containing the
jar
command in the PATH environment variable
on your machine. On Windows, if you install the JDK in the
C:\java8
directory, the
jar
command is stored in the
C:\java8\bin
directory.
In the above command, the option
c
indicates that you are creating a new JAR file and the option
f
indicates that
you are specifying a JAR file name, which is
test.jar
. At the end of the command, you can specify one or more file
names or directory names to include in the JAR file.
To view the contents of the
test.jar
file, you can execute the following command:
jar tf test.jar
The option
t
in this command indicates that you are interested in the table of contents of a JAR file. The option
f
indicates that you are specifying the JAR file name, which is
test.jar
in this case. The above command will
generate the following output:
META-INF/
META-INF/MANIFEST.MF
A.class
B.class
Note that the
jar
command had automatically created two extra things for you: one directory called
META-INF
and a file named
MANIFEST.MF
in the
META-INF
directory.
The following command will create a
test.jar
file by including everything in the current working directory. Note
the use of an asterisk as the wild-card character to denote everything in the current working directory.
jar cf test.jar *