Setting an Application's Entry Point
If you have an application bundled in a JAR file, you need some way to indicate which class within the JAR file is your application's entry point. You provide this information with the
Main-Classheader in the manifest, which has the general form:Main-Class: classnameThe value
classnameis the name of the class that is your application's entry point.Recall that the entry point is a class having a method with signature
public static void main(String[] args).After you have set the
Main-Classheader in the manifest, you then run the JAR file using the following form of thejavacommand:java -jar JAR-nameThe
mainmethod of the class specified in theMain-Classheader is executed.An Example
We want to execute the
mainmethod in the classMyClassin the packageMyPackagewhen we run the JAR file.We first create a text file named
Manifest.txtwith the following contents:Main-Class: MyPackage.MyClass
Warning:The text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.
We then create a JAR file named
MyJar.jarby entering the following command:jar cfm MyJar.jar Manifest.txt MyPackage/*.classThis creates the JAR file with a manifest with the following contents:
Manifest-Version: 1.0 Created-By: 1.6.0 (Sun Microsystems Inc.) Main-Class: MyPackage.MyClassWhen you run the JAR file with the following command, the
mainmethod ofMyClassexecutes:java -jar MyJar.jarSetting an Entry Point with the JAR Tool
The 'e' flag (for 'entrypoint'), introduced in JDK 6, creates or overrides the manifest's
Main-Classattribute. It can be used while creating or updating a jar file. Use it to specify the application entry point without editing or creating the manifest file.
For example, this command createsapp.jarwhere theMain-Classattribute value in the manifest is set toMyApp:jar cfe app.jar MyApp MyApp.classYou can directly invoke this application by running the following command:
java -jar app.jarIf the entrypoint class name is in a package it may use a '.' (dot) character as the delimiter. For example, if
Main.classis in a package calledfoothe entry point can be specified in the following ways:jar cfe Main.jar foo.Main foo/Main.class
浙公网安备 33010602011771号