问题描述
我在IDEA Build> Build Artifacts中创建jar文件.但是不能用java -jar jarname.jar
-Error: Could not find or load main class Hello
运行它. MANIFEST.MF文件在/resources/META-INF/文件夹中.这是启动器类:
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:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
MANIFEST.MF:
MANIFEST.MF:
Manifest-Version: 1.0
Main-Class: Hello
添加了工件设置屏幕截图
推荐答案
您转到项目结构,然后从左侧选项卡中选择工件".添加一个新的工件,尽管您选择了清单和Main类,但是在这里您可以看到,在左侧,它显示了将添加到jar中的内容.在右侧,它显示了可用的(未添加).但是,由于我还没有在jar中添加任何内容,因此只会添加Manifest,而不会添加任何实际代码.
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.
您需要指定要将编译输出添加到jar中,否则它将仅包含清单而不包含您的实际类.您可以通过双击它来做到这一点.如果您在编译后的输出上方指定目录,那么我相信也会添加src.
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文件:找不到或加载主类Hello的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!