简体   繁体   English

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

[英]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.

相关问题 由于“无法找到或加载主类”而无法运行jar文件 - Cannot run jar file because of “Could not find or load main class” 找不到或加载主类-我无法运行从Ant创建的.jar文件 - Could not find or load main class- I cannot run my .jar file created from Ant 无法使用 Jar 文件找到或加载主 class - Could not find or load main class with a Jar File 错误:找不到或加载主类Hello - Error: could not find or load main class Hello “你好,世界!” - 无法找到或加载主类 - “Hello, world!” - Could not find or load main class 运行带有“错误:无法找到或加载主类”的 jar - run jar with `Error: Could not find or load main class` eclipse无法运行主程序“找不到或加载主类错误” - eclipse cannot run main “error could not find or load main class” 无法弄清楚如何运行我的jar文件“找不到或加载主类” - Can't figure out how to run my jar file “could not find or load main class” 无法运行 jar 文件,出现无法找到或加载主类(Maven 项目)的错误 - Unable to run jar file, getting error as Could not find or load main class (Maven Project) 无法运行jar文件,出现“错误:找不到或加载主类com.trident.SchedulerMain” - Unable to run jar file getting “Error: Could not find or load main class com.trident.SchedulerMain”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM