“找不到主要类:XX。程序将退出。“ [英] "Could not find the main class: XX. Program will exit."

查看:78
本文介绍了“找不到主要类:XX。程序将退出。“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法用命令提示符运行我的jar文件,但它总是给我一个响应

I have managed to run my jar file with a command prompt, but its always giving me a reponse of


找不到主要类:XX。程序将退出。

Could not find the main class: XX. Program will exit.

请帮助我,谢谢。

推荐答案

请参阅设置应用程序的入口点


如果你有一个捆绑在JAR文件中的应用程序,你需要一些方法来指示JAR文件中的哪个类是您的应用程序的入口点。您使用清单中的Main-Class标头提供此信息,其标题为:

If you have an application bundled in a JAR file, you need some way to indicate which class within the JAR file is your application's entry point. You provide this information with the Main-Class header in the manifest, which has the general form:



Main-Class: classname




值classname是名称作为应用程序入口点的类。

The value classname is the name of the class that is your application's entry point.

回想一下,入口点是一个具有签名方法的类

Recall that the entry point is a class having a method with signature



 public static void main(String[] args).

在清单中设置Main-Class标头后,然后使用以下命令运行JAR文件以下形式的java命令:

After you have set the Main-Class header in the manifest, you then run the JAR file using the following form of the java command:

java -jar JAR-name

执行Main-Class标头中指定的类的主要方法。

The main method of the class specified in the Main-Class header is executed.


我们首先创建一个名为Manifest.txt的文本文件,其中包含以下内容:

We first create a text file named Manifest.txt with the following contents:



Main-Class: MyPackage.MyClass




警告:文本文件必须以新行或回车结尾。如果不以新行或回车结束,则不会正确解析最后一行。

Warning: The text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.

然后输入以下命令创建一个名为MyJar.jar的JAR文件:

We then create a JAR file named MyJar.jar by entering the following command:



jar cfm MyJar.jar Manifest.txt MyPackage/*.class




这将创建一个带有以下内容的清单的JAR文件:

This creates the JAR file with a manifest with the following contents:



Manifest-Version: 1.0
Created-By: 1.6.0 (Sun Microsystems Inc.)
Main-Class: MyPackage.MyClass




使用以下命令运行JAR文件时,MyClass的main方法执行:

When you run the JAR file with the following command, the main method of MyClass executes:



java -jar MyJar.jar

这篇关于“找不到主要类:XX。程序将退出。“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆