Java Reference
In-Depth Information
Note that when you specify the option
m
, you must also specify the manifest file name. The order in which you
specify the new JAR file name and the manifest file name must match the order of options
m
and
f
. For example, you
can change the above command by specifying the
f
and
m
options in a different order as follows:
jar cmf manifest.txt test.jar *.class
This command will add a manifest file with the following contents to the
test.jar
file:
Manifest-Version: 1.0
Created-By: 1.8.0_20-ea (Oracle Corporation)
Main-Class: com.jdojo.intro.Welcome
If you do not specify the
Manifest-Version
and
Created-By
attribute in your manifest file, the tool adds them. It
defaults the
Manifest-Version
to 1.0. The
Created-By
is defaulted to the JDK version you use.
You have been running a Java program by using the
java
command and specifying the class name that has the
main()
method as follows:
java com.jdojo.intro.Welcome
You can also run a
jar
file using the
-jar
option with the
java
command as follows:
java -jar test.jar
When you run the above command, the JVM will look for the value of the
Main-Class
attribute in the
MANIFEST.MF
file in the
test.jar
file and attempt to run that class. If you have not included a
Main-Class
attribute in
the
test.jar
file, the above command will generate an error.
You can also add the
Main-Class
attribute value in the manifest file without creating your own manifest file. Use
the option
e
with the jar tool when you create/update a jar file. The following command will add
com.jdojo.intro.
Welcome
as the value of the
Main-Class
in the
MANIFEST.MF
file in the
test.jar
file:
jar cfe test.jar com.jdojo.intro.Welcome *.class
The following command will add
com.jdojo.intro.Welcome
as the value of the
Main-Class
in the
MANIFEST.MF
file in an existing
test.jar
file by using the option
u
for update:
jar ufe test.jar com.jdojo.intro.Welcome
You can set the CLASSPATH for a JAR file in its manifest file. The attribute name is called
Class-Path
, which you
must specify in a custom manifest file. It is a space-separated list of jar files, zip files, and directories. The
Class-Path
attribute in a manifest file looks like
Class-Path: chapter8.jar file:/c:/book/
http://www.jdojo.com/jutil.jar
The above entry has three items for the CLASSPATH: a JAR file
chapter8.jar
, a directory using the file protocol
file:/c:/book/
, and another JAR file using a HTTP protocol
http://www.jdojo.com/jutil.jar
.
Note that a
directory name must end with a forward slash. Suppose this
Class-Path
setting is included in the manifest file for the
test.jar
file. When you run the
test.jar
file using the following
java
command, this CLASSPATH will be used to
search and load classes.
java -jar test.jar