java 无法运行 jar 文件:找不到或无法加载主类 Hello

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

Cannot run jar file: Could not find or load main class Hello

javaintellij-ideagradlejar

提问by Mansur Nashaev

I create jar file in IDEA Build>Build Artifacts. But can't run it with java -jar jarname.jar- Error: Could not find or load main class Hello. MANIFEST.MF file is in the /resources/META-INF/ folder. And here is the launcher class:

我在 IDEA Build>Build Artifacts 中创建了 jar 文件。但是不能用java -jar jarname.jar-运行它Error: Could not find or load main class Hello。MANIFEST.MF 文件位于 /resources/META-INF/ 文件夹中。这是启动器类:

public class Hello {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }

}

MANIFEST.MF:

清单.MF:

Manifest-Version: 1.0
Main-Class: Hello

enter image description here

在此处输入图片说明



EDIT: Added artifacts setting screenshot

编辑:添加工件设置屏幕截图

enter image description here

在此处输入图片说明

回答by Brion

enter image description here

在此处输入图片说明

You go to project structure then choose "Artifacts" from the left tab. Add a new artifact and as you see here although I have a manifest selected and a Main class selected, on the left side it shows what it will add to the jar. On the right side it shows what's available(un-added). However, since I haven't added anything into my jar yet, it will only add the Manifest and none of the actual code.

您转到项目结构,然后从左侧选项卡中选择“工件”。添加一个新工件,正如您在此处看到的,虽然我选择了一个清单并选择了一个 Main 类,但在左侧它显示了它将添加到 jar 中的内容。在右侧,它显示了可用的内容(未添加)。但是,由于我还没有在 jar 中添加任何内容,因此它只会添加 Manifest 而没有添加任何实际代码。

You need to specify that you want to add the compile output to the jar or else it will only have the manifest and not your actual classes. You can do that by just double clicking on it. If you specify the directory above the compiled output, it will add the src as well I believe.

您需要指定要将编译输出添加到 jar 中,否则它将只有清单而不是您的实际类。你可以通过双击它来做到这一点。如果您在编译输出上方指定目录,我相信它也会添加 src 。

Update: Add external dependencies

更新:添加外部依赖项

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

回答by loesak

i believe your manifest file must say what the main class is if you want it to auto execute.

我相信如果您希望它自动执行,您的清单文件必须说明主类是什么。

Main-Class: Hello

主班:你好

otherwise you need to specify it on the command line when attempting to execute the jar. As far as how to do that with IntelliJ, I can't help you there.

否则,您需要在尝试执行 jar 时在命令行上指定它。至于如何使用 IntelliJ 做到这一点,我无法帮助您。

java -cp hello.jar Hello

java -cp hello.jar 你好

Note that the reference to the class with the main method is the fully qualified location (package.classname) but since your class has the default package, its not necessary.

请注意,使用 main 方法对类的引用是完全限定的位置 (package.classname),但由于您的类具有默认包,因此没有必要。

回答by reihaneh

If your jar file build correctly. try java -jar hello.jar Hello

如果您的 jar 文件构建正确。试试java -jar hello.jar 你好