You can run a jar file using the ?jar option with the java command as follows:
java -jar test.jar
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 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.book2s.intro.Welcome as the value of the Main-Class in the MANIFEST.MF file in the test.jar file:
jar cfe test.jar com.book2s.intro.Welcome *.class
The following command will add com.book2s.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.book2s.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: testjar.jar file:/c:/book/ http://www.book2s.com/util.jar
The above entry has three items for the CLASSPATH: a JAR file testjar.jar, a directory using the file protocol file:/c:/book/, and another JAR file using a HTTP protocol http://www.book2s.com/util.jar.
A directory name must end with a forward slash.
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
When you run a JAR file with the ?jar option using the java command, any CLASSPATH setting outside the manifest file of the JAR file (test.jar file) is ignored.
Using the Class-Path attribute you can generate an index of all packages using the option i of the jar tool.
The following command generates an index for all packages in all JAR files listed in the Class-Path attribute of the manifest file in the test.jar file:
jar i test.jar