jes516 jes516 - 1 month ago 21
Java Question

how to set main-class and class-path in manifest file for java

I am trying to get a grasp on Main-Class and Class-Path within a manifest file for java. How come you do not include "C:/" or "/" as part of the path for Main-Class or Class-Path? All of the examples I have seen so far start with a directory name ie. com/test/path or lib/test/path etc...

For example if my java project is in

d:\java\sample_one\
and my .java and .class file are in
sample_one
can my Class-Path be set to
d:\java\sample_one\
?

Also, why do the paths in Main-Class and Class-Path in some of the various examples I have seen use periods
.
instead of f/b slashes
/\
?

any help is greatly appreciated. thank you

Answer

The Main-Class attribute in a java manifest file is designed to be used with the java -jar foo.jar invocation. It tells the java command what class within the Jar file should have its main method executed when using the above command line syntax. It basically avoids having to explicitly specify the main class on the command line.

A key thing to remember is that the Main-Class refers to a class WITHIN THE JAR FILE, not on the file system.

Similarly, the Class-Path attribute allows you to specify java jar files that should be on the classpath. Again, it avoids having to specify them on the command line with the -classpath parameter. Handy if you are packaging an application with supporting jar files and you want to keep the launching syntax simple.

In contrast to the Main-Class attribute, the entries here DO refer to JAR files in the file system.

The dots in the Main-Class identify the package that the class resides in. It uses the same syntax convention as a package statement in a class, or an import statement. Think of it as an alternate syntax for the directory structure of your code.

For example, if your code is structured to use a package name com.acme and the main class to run is named Checkout, then the Main-Class attribute would be com.acme.Checkout, and the Checkout.class file would be within the com/acme subdirectory in the JAR file.