简体   繁体   English

“找不到主要类别:XX。 程序将会退出。”

[英]“Could not find the main class: XX. Program will exit.”

I have managed to run my jar file with a command prompt, but its always giving me a reponse of 我已经设法在命令提示符下运行jar文件,但是它总是给我以下响应

Could not find the main class: XX. 找不到主要类别:XX。 Program will exit. 程序将会退出。

Please help me out, thanks. 请帮帮我,谢谢。

See Setting an Application's Entry Point 请参阅设置应用程序的入口点

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. 如果您的应用程序捆绑在JAR文件中,则需要某种方式来指示JAR文件中的哪个类是应用程序的入口点。 You provide this information with the Main-Class header in the manifest, which has the general form: 您可以在清单中的Main-Class标头中提供此信息,该标头具有以下一般形式:

Main-Class: classname

The value classname is the name of the class that is your application's entry point. 值classname是作为应用程序入口点的类的名称。

Recall that the entry point is a class having a method with signature 回想一下,入口点是具有签名方法的类

 public static void main(String[] args).

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: 在清单中设置Main-Class标头之后,然后使用以下形式的java命令运行JAR文件:

java -jar JAR-name

The main method of the class specified in the Main-Class header is executed. 执行Main-Class标头中指定的类的main方法。


We first create a text file named Manifest.txt with the following contents: 我们首先创建一个名为Manifest.txt的文本文件,其内容如下:

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. 如果最后一行未以新行或回车结尾,则将无法正确解析。

We then create a JAR file named MyJar.jar by entering the following command: 然后,通过输入以下命令,创建一个名为MyJar.jar的JAR文件:

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

This creates the JAR file with a manifest with the following contents: 这将创建带有清单的JAR文件,其中包含以下内容:

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

When you run the JAR file with the following command, the main method of MyClass executes: 使用以下命令运行JAR文件时,将执行MyClass的main方法:

java -jar MyJar.jar

I had the same error. 我有同样的错误。 The problem was that Windows 10 suddenly decided to set my workspace folder to read-only. 问题是Windows 10突然决定将我的工作区文件夹设置为只读。

After removing the read-only checkmark in the folder's options, the problem was solved. 删除文件夹选项中的只读复选标记后,问题已解决。

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

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