Java “找不到主类:XX。程序将退出。”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1417328/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:52:15  来源:igfitidea点击:

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

javamain

提问by

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. Program will exit.

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

Please help me out, thanks.

请帮帮我,谢谢。

回答by VonC

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. You provide this information with the Main-Class header in the manifest, which has the general form:

如果您的应用程序捆绑在 JAR 文件中,您需要某种方式来指明 JAR 文件中的哪个类是您的应用程序的入口点。您在清单中的 Main-Class 标头中提供此信息,其具有一般形式:

Main-Class: 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

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

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

 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

回答by Christian Napierala

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.

去掉文件夹选项中的只读复选标记后,问题就解决了。