Main-Class is not written to manifest of a jar

…衆ロ難τιáo~ 提交于 2021-02-10 17:59:29

问题


when I create a jar file from .class files with either:

jar cmvf META-INF/MANIFEST.MF MyApp.jar *.class

or

jar cmvf manifest.mf MyApp.jar *.class

despite a line with main class in the manifest file:

Manifest-Version: 1.0

Created-By: 1.8.0_171 (Oracle Corporation)

Main-Class: MainClassName

I get an error when I start MyApp.jar

java -jar MyApp.jar

no main manifest attribute, in MyApp.jar

when i unpack the jar (in some other folder) with:

jar xf MyApp.jar

the manifest looks like this:

Manifest-Version: 1.0

Created-By: 1.8.0_171 (Oracle Corporation)

why main class line disappears? "MainClassName.class" is in this folder and it has main function in it. Here is the output when i create the jar:

added manifest

adding: SomeClass.class(in = 1106) (out= 472)(deflated 57%)

... other classes ...

Thanks in advance.

[SOLUTION] thanks to dave

(Especially on Windows) Make sure the last line, here the Main-class: line, has a line terminator aka newline (LF or CRLF). If the last line is unterminated it is ignored. (Officially there should actually be a second newline, thus an empty line, at the end of the manifest main-section, but IME it works without that.)


回答1:


Starting in Java 8, the manifest is written after the jar name:

jar cmvf MyApp.jar META-INF/MANIFEST.MF *.class

Java 7
Java 8



来源:https://stackoverflow.com/questions/51368363/main-class-is-not-written-to-manifest-of-a-jar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!