开发者

running java [-options] -jar jarfile (no explicit classname given)

Please correct me if I am wrong.

When running java [-options] -jar jarfile with no explicit class name, if a single class exists within the jar that contains a public static void main(String[] args) method, t开发者_如何学编程hat method will be invoked automatically. Right?

What happens if I have several classes that contain an eligible main method?


It uses the Main-Class defined in the MANIFEST file to determine which class contains the main method that should be run.

see http://java.sun.com/docs/books/tutorial/deployment/jar/appman.html


The class to run is taken from the Manifest metadata.

Main-Class :

The value of this attribute defines the relative path of the main application class which the launcher will load at startup time. The value must not have the .class extension appended to the class name.


The JAR must contain a manifest file with a "Main-Class" entry.

Edit: I recall at one point the docs were incorrect. If "Main-Class" doesn't work for you, try "Main-class" (lowercase "class").


Which class' main is run is determined by the MANIFEST file inside the .jar. Specifically the Main-Class attribute.


If you attempt to use java -jar on a jar without the above said manifest, you'll get the following output:

Failed to load Main-Class manifest attribute from <jarname>.jar
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜