Java 为什么无法从 JAR 文件加载主类清单属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2591516/
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
Why has it failed to load main-class manifest attribute from a JAR file?
提问by Roman
I have created a JAR file in this way jar cf jar-file input-files
. Now, I'm trying to run it. Running it does not work (jre command is not found):
我以这种方式创建了一个 JAR 文件jar cf jar-file input-files
。现在,我正在尝试运行它。运行它不起作用(找不到 jre 命令):
jre -cp app.jar MainClass
This does not work either:
这也不起作用:
java -jar main.jar
(Failed to load Main-Class manifest attribute from main.jar).
(无法从 main.jar 加载主类清单属性)。
I also found out that
我也发现
To run an application packaged as a JAR file (version 1.2 -- requires Main-Class manifest header)
运行打包为 JAR 文件的应用程序(版本 1.2 -- 需要 Main-Class 清单标头)
What is the "Main-Class manifest header"? How do I create it and where do I put it?
什么是“主类清单标头”?我如何创建它以及我把它放在哪里?
采纳答案by Jon Skeet
I'm not sure I believe your symptoms:
我不确定我是否相信你的症状:
- If the
jre
command isn't found, then runningjre -cp app.jar
should give the same error - Just adding a JARfile to the classpath shouldn't give the error you're seeing
- 如果
jre
未找到该命令,则运行jre -cp app.jar
应给出相同的错误 - 只是将JAR文件添加到类路径不应该给出您看到的错误
I'd expect you to see this error if you run:
如果您运行,我希望您看到此错误:
java -jar app.jar
The Main-Class header needs to be in the manifest for the JAR file - this is metadata about things like other required libraries. See the Sun documentationfor how to create an appropriate manifest. Basically you need to create a text file which includes a line like this:
Main-Class 标头需要在 JAR 文件的清单中 - 这是有关其他所需库等内容的元数据。有关如何创建适当清单的信息,请参阅Sun 文档。基本上,您需要创建一个文本文件,其中包含如下一行:
Main-Class: MainClass
Then run
然后运行
jar cfm app.jar manifest.txt *.class
回答by Dainius
You can run with:
你可以运行:
java -cp .;app.jar package.MainClass
It works for me if there is no manifest in the JAR file.
如果 JAR 文件中没有清单,它对我有用。
回答by weirdFactory
set the classpath and compile
javac -classpath "C:\Program Files\Java\jdk1.6.0_updateVersion\tools.jar" yourApp.java
create manifest.txt
Main-Class: yourApp newline
create yourApp.jar
jar cvf0m yourApp.jar manifest.txt yourApp.class
run yourApp.jar
java -jar yourApp.jar
设置类路径并编译
javac -classpath "C:\Program Files\Java\jdk1.6.0_ updateVersion\tools.jar" yourApp.java
创建清单.txt
主类: yourApp换行符
创建 yourApp.jar
jar cvf0m yourApp.jar manifest.txt yourApp.class
运行 yourApp.jar
java -jar yourApp.jar
回答by Greg Burdett
The easiest way to be sure that you have created the runnable JAR file correctly, with the appropriate manifest file, is to use Eclipseto build it for you. In your Eclipse project, you basically just select File/Export from the menu, and follow the prompts.
确保使用适当的清单文件正确创建了可运行 JAR 文件的最简单方法是使用Eclipse为您构建它。在您的 Eclipse 项目中,您基本上只需从菜单中选择 File/Export,然后按照提示进行操作。
That way, you can be sure that your JAR file is correct and will know to look elsewhere if there is still an issue. The process is described in full in FAQ How do I create an executable JAR file for a stand-alone SWT program?.
这样,您就可以确定您的 JAR 文件是正确的,并且如果仍然存在问题,就会知道去别处寻找。该方法在充分描述常见问题如何创建一个独立的SWT程序可执行的JAR文件?.
回答by Sridhar Sarnobat
I got this error, and it was because I had the arguments in the wrong order:
我收到此错误,这是因为我的参数顺序错误:
CORRECT
正确的
java maui.main.Examples tagging -jar maui-1.0.jar
WRONG
错误的
java -jar maui-1.0.jar maui.main.Examples tagging
回答by john p
I discovered that I was also having this error in NetBeans. I hope the following is helpful.
我发现我在 NetBeans 中也遇到了这个错误。我希望以下内容会有所帮助。
- Make sure that when you go to Project Configuration you set the main class you intend for running.
- Do a Build or Clean Build
- Place the jar file where you wish and try: java -jar "YourProject.jar" again at the command line.
- 确保在转到项目配置时设置要运行的主类。
- 进行构建或清理构建
- 将 jar 文件放在您希望的位置,然后在命令行中再次尝试: java -jar "YourProject.jar"。
This was the problem I was getting because I had other "test" programs I was using in NetBeans and I had to make sure the Main Class under the Run portion of the Project configuration was set correctly.
这是我遇到的问题,因为我在 NetBeans 中使用了其他“测试”程序,我必须确保项目配置的运行部分下的主类设置正确。
many blessings, John P
很多祝福,约翰 P
回答by ObviousChild
I was getting the same error when i ran:
jar cvfm test.jar Test.class Manifest.txt
我在运行时遇到了同样的错误:
jar cvfm test.jar Test.class Manifest.txt
What resolved it was this:
jar cvfm test.jar Manifest.txt Test.class
解决它的是这样的:
jar cvfm test.jar Manifest.txt Test.class
My manifest has the entry point as given in oracle docs (make sure there is a new line character at the end of the file):
Main-Class: Test
我的清单具有 oracle 文档中给出的入口点(确保文件末尾有一个换行符):
Main-Class: Test
回答by user4581964
Try
尝试
java -cp .:mail-1.4.1.jar JavaxMailHTML
no need to have manifest
file.
不需要有manifest
文件。
回答by Harshita Sethi
I faced the same problem. This unix command is not able to find the main class. This is because the runtime and compile time JDK versions are different. Make the jar through eclipse after changing the java compiler version. The following link helped me.
我遇到了同样的问题。此 unix 命令无法找到主类。这是因为运行时和编译时 JDK 版本不同。更改java编译器版本后通过eclipse制作jar。以下链接帮助了我。
Try running the jar created after this step and then execute it
尝试运行这一步之后创建的jar然后执行
回答by AmirHossein Rezaei
If your class path is fully specified in manifest, maybe you need the last versionof java runtime environment. My problem fixed when i reinstalled the jre 8.
如果您的类路径在清单中完全指定,那么您可能需要最新版本的 Java 运行时环境。当我重新安装 jre 8 时,我的问题得到了解决。