[英]Cannot run jar file: Could not find or load main class Hello
I create jar file in IDEA Build>Build Artifacts. 我在IDEA Build> Build Artifacts中创建jar文件。 But can't run it with
java -jar jarname.jar
- Error: Could not find or load main class Hello
. 但是不能使用
java -jar jarname.jar
来运行它- Error: Could not find or load main class Hello
。 MANIFEST.MF file is in the /resources/META-INF/ folder. MANIFEST.MF文件在/ resources / META-INF /文件夹中。 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
EDIT: Added artifacts setting screenshot 编辑:添加了工件设置屏幕截图
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.
添加一个新的工件,尽管您选择了清单和Main类,但是如您在此处看到的那样,在左侧显示了它将添加到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中添加任何内容,因此只会添加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. 您需要指定要将编译输出添加到jar中,否则它将仅包含清单而不包含您的实际类。 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.
如果您在编译输出上方指定目录,那么我相信也会添加src。
Update: Add external dependencies 更新:添加外部依赖项
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. 否则,在尝试执行jar时需要在命令行上指定它。 As far as how to do that with IntelliJ, I can't help you there.
至于如何使用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),但是由于您的类具有默认包,因此不必要。
If your jar file build correctly. 如果您的jar文件正确构建。 try java -jar hello.jar Hello
试试java -jar hello.jar你好
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.