Setting an Application's Entry Point
3765 단어 application
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-Class
header in the manifest, which has the general form:
Main-Class: classname
The value classname
is 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-Class
header in the manifest, you then run the JAR file using the following form of the java
command:
java -jar JAR-name
The main
method of the class specified in the Main-Class
header is executed.
An Example
We want to execute the main
method in the class MyClass
in the package MyPackage
when we run the JAR file.
We first create a text file named Manifest.txt
with 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.jar
by entering the following command:
jar cfm MyJar.jar Manifest.txt MyPackage/*.class
This 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.MyClass
When you run the JAR file with the following command, the main
method of MyClass
executes:
java -jar MyJar.jar
Setting an Entry Point with the JAR Tool
The 'e' flag (for 'entrypoint'), introduced in JDK 6, creates or overrides the manifest's Main-Class
attribute. 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 creates app.jar
where the Main-Class
attribute value in the manifest is set to MyApp
:
jar cfe app.jar MyApp MyApp.class
You can directly invoke this application by running the following command:
java -jar app.jar
If the entrypoint class name is in a package it may use a '.' (dot) character as the delimiter. For example, if Main.class
is in a package called foo
the entry point can be specified in the following ways:
jar cfe Main.jar foo.Main foo/Main.class
« Previous •
Trail •
Next »
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Pre-Query Samples
Validate the current query criteria or provide additional query criteria programmatically, just before sending the SELEC...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.
Main-Class: classname
java -jar JAR-name
Main-Class: MyPackage.MyClass
jar cfm MyJar.jar Manifest.txt MyPackage/*.class
Manifest-Version: 1.0
Created-By: 1.6.0 (Sun Microsystems Inc.)
Main-Class: MyPackage.MyClass
java -jar MyJar.jar
jar cfe app.jar MyApp MyApp.class
java -jar app.jar
jar cfe Main.jar foo.Main foo/Main.class
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Pre-Query SamplesValidate the current query criteria or provide additional query criteria programmatically, just before sending the SELEC...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.