简体   繁体   English

创建并执行 JAR 文件时出错

[英]Error while creating JAR file and executing it

I have a problem creating and executing a JAR file.我在创建和执行 JAR 文件时遇到问题。 I have already made a JAR file, but when I execute it with java -jar , I am getting an error Error: could not find and load main class... I make a JAR file with jar cvfm , but I execute it from C:\Program Files\Java\jdk1.70\我已经制作了一个 JAR 文件,但是当我使用java -jar执行它时,出现错误Error: could not find and load main class...我使用jar cvfm制作了一个 JAR 文件,但我从 85561 执行它:\程序文件\Java\jdk1.70\

What's wrong with this?这有什么问题吗?

To create an executable jar file you have to specify the entry point to the jar.Like this:要创建可执行的 jar 文件,您必须指定 jar 的入口点。像这样:

jar -cvfe "jar file name" "Main Class Name(Ex com.test.MainTest)" "Files to be included in the jar" jar -cvfe "jar file name" "Main Class Name(Ex com.test.MainTest)" "要包含在jar中的文件"

If you already have a jar file, you can update the manifest file by creating an "additions" file and running the command to include the main class:如果您已经有一个 jar 文件,您可以通过创建一个“additions”文件并运行命令以包含主要的 class 来更新清单文件:

Main-Class: Classname主类:类名

and run command: jar ufm "jarfilename" "additions manifest"并运行命令:jar ufm "jarfilename" "additions manifest"

Maybe an entry in your manifest is missing?也许您的清单中的条目丢失了? You have to add your MainClass to the MANIFEST.MF - the entry needed is Main-Class: classname您必须将 MainClass 添加到 MANIFEST.MF - 所需的条目是Main-Class: classname

For mor informations see here有关更多信息,请参见此处

Whenever we create the jar file, we pass the main-class parameter in Manifest.mf that is embed in the jar.每当我们创建 jar 文件时,我们都会在 jar 中嵌入的 Manifest.mf 中传递主类参数。

you have missed that part and now when you are executing its not able to identify the main class to execute from你错过了那部分,现在当你执行它时无法识别要从中执行的主要 class

http://www.skylit.com/javamethods/faqs/createjar.html might help http://www.skylit.com/javamethods/faqs/createjar.html可能有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM