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

后端 未结 3 1065
太阳男子
太阳男子 2021-01-20 04:53

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. MAN

相关标签:
3条回答
  • 2021-01-20 05:16

    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.

    java -cp hello.jar Hello

    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.

    0 讨论(0)
  • 2021-01-20 05:20

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

    0 讨论(0)
  • 2021-01-20 05:31

    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.

    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.

    Update: Add external dependencies

    0 讨论(0)
提交回复
热议问题