Java 无法加载主类清单属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3991936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Failed to load Main-Class manifest attribute
提问by Gabriele Cirulli
I know this is a very common question, but I tried googling and got no helpful result. I'm trying to make a jar out of my "Main.class" file (made in NetBeans, but I'm not trying to make the jar with it)
我知道这是一个非常常见的问题,但我尝试使用谷歌搜索并没有得到任何有用的结果。我正在尝试用我的“Main.class”文件制作一个罐子(在 NetBeans 中制作,但我不想用它制作罐子)
I ran this command:
我运行了这个命令:
D:\NetBeans\trovanum3\build\classes\trovanum3>jar cf trovanum.jar *.class
And a .jar file spawned in that folder. I thought that would've worked, but it won't run.
并在该文件夹中生成了一个 .jar 文件。我认为这会奏效,但它不会运行。
I tried opening the jar file with an archive opener and inside it are the "Main.class" file and a "META-INF" folder containing a file named "MANIFEST.MF" The file contains this:
我尝试使用存档打开器打开 jar 文件,其中包含“Main.class”文件和“META-INF”文件夹,其中包含一个名为“MANIFEST.MF”的文件。该文件包含以下内容:
Manifest-Version: 1.0
Created-By: 1.6.0_22 (Sun Microsystems Inc.)
What could be the problem?
可能是什么问题呢?
采纳答案by martin clayton
You can use the -e
option of jarto specify the entry point, i.e. the main class. For example:
可以使用jar的-e
选项来指定入口点,即主类。例如:
D:\NetBeans\trovanum3\build\classes> jar cfe trovanum.jar trovanum3.Main trovanum3\*.class
Saves fiddling with the manifest if that's all you need in there. Note the working directory is one upfrom that quoted in your question. Otherwise the Main class is created at the top-level in the jar, rather than in a directory that reflects the package in which it should reside. jar -tvf
should then show something like this:
如果这就是你所需要的,就可以省去摆弄清单的工作。请注意,工作目录是您问题中引用的目录之一。否则 Main 类将在 jar 的顶层创建,而不是在反映它应该驻留的包的目录中。 jar -tvf
然后应该显示如下内容:
0 Thu Oct 21 22:34:30 BST 2010 META-INF/
80 Thu Oct 21 22:34:30 BST 2010 META-INF/MANIFEST.MF
488 Thu Oct 21 22:18:24 BST 2010 trovanum3/Main.class
-e
Sets entrypoint as the application entry point for stand-alone applications bundled into executable jar file. The use of this option creates or overrides the Main-Class attribute value in the manifest file. This option can be used during creation of jar file or while updating the jar file. This option specifies the application entry point without editing or creating the manifest file.
-e
将入口点设置为捆绑到可执行 jar 文件中的独立应用程序的应用程序入口点。使用此选项会创建或覆盖清单文件中的 Main-Class 属性值。此选项可在创建 jar 文件或更新 jar 文件时使用。此选项指定应用程序入口点,而无需编辑或创建清单文件。
回答by Mark Peters
You need to specify the Main-Class property in the manifest file: Understanding the Manifest. This usually means you need to add the manifest file yourself instead of letting it be added automatically.
您需要在清单文件中指定 Main-Class 属性:Understanding the Manifest。这通常意味着您需要自己添加清单文件,而不是让它自动添加。
By the way you can always run your code without a manifest like this:
顺便说一句,您始终可以在没有清单的情况下运行您的代码,如下所示:
java -cp path/to/myJar.jar my.package.MyClass
The manifest is required when you want to run it like this:
当你想像这样运行它时,清单是必需的:
java -jar path/to/myJar.jar
回答by Leo Izen
Extract the mainifest file.
提取 mainifest 文件。
Add an extra line so it says :
添加一个额外的行,所以它说:
Manifest-Version: 1.0
Created-By: 1.6.0_22 (Sun Microsystems Inc.)
Main-Class: Main
Make sure there is two newline characters at the end.
确保末尾有两个换行符。
Update the jar file. You could either have edited the Manifest in the jar file with WinRAR, 7-zip, etc. or you could have deleted the original jar (after extracting and editing) then ran
更新 jar 文件。您可以使用 WinRAR、7-zip 等编辑 jar 文件中的清单,或者您可以删除原始 jar(在提取和编辑后)然后运行
jar -cmf MANIFEST.MF trovanum.jar *.class
Then it would use your newly modified manifest in the jar.
然后它将在 jar 中使用您新修改的清单。
回答by Peter C
Create a file called well, anything really, but we'll call it manifest.txt
创建一个名为 well 的文件,任何真的,但我们将其命名为 manifest.txt
Open manifest.txt and type:
打开 manifest.txt 并输入:
Main-Class: Main
then to create your jar, type
然后创建你的罐子,输入
jar cfm trovanum.jar manifest.txt *.class
And it should run fine.
它应该运行良好。
回答by kkress
You need to set the Main-Class attribute in the manifest of your jar. See the java tutorial link below for details.
您需要在 jar 的清单中设置 Main-Class 属性。有关详细信息,请参阅下面的 java 教程链接。
http://download.oracle.com/javase/tutorial/deployment/jar/appman.html
http://download.oracle.com/javase/tutorial/deployment/jar/appman.html
回答by Kanika
Create executable jar( and not plain jar file). You get this option in eclipse while exporting jar file. I was facing same issue and it resolved once I created the executable jar.
创建可执行的 jar(而不是普通的 jar 文件)。导出 jar 文件时,您可以在 eclipse 中获得此选项。我遇到了同样的问题,一旦我创建了可执行 jar,它就解决了。
Create batch file as : java -jar "abc.jar" ..
创建批处理文件为: java -jar "abc.jar" ..