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

19,419

Solution 1

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.

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

enter image description here

enter image description here

Solution 2

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.

Share:
19,419
Mansur Nashaev
Author by

Mansur Nashaev

Updated on June 05, 2022

Comments

  • Mansur Nashaev
    Mansur Nashaev almost 2 years

    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-Version: 1.0
    Main-Class: Hello
    

    enter image description here


    EDIT: Added artifacts setting screenshot

    enter image description here

  • Mansur Nashaev
    Mansur Nashaev over 7 years
    with java -cp hello.jar Hello i got Error: Could not find or load main class Hello
  • loesak
    loesak over 7 years
    then you need to verify that your jar was created correctly, meaning is the Hello.class in your jarfile
  • Mansur Nashaev
    Mansur Nashaev over 7 years
    It works. But when I added the apache io library fot the test and launched .jar, an error occurs: java.lang.NoClassDefFoundError: org/apache/commons/io/FileUtils at Hello.main(Hello.java:10) I understand that the issue of including libraries, but do not know how to include them
  • Brion
    Brion over 7 years
    org.apache.commons.io.FileUtils is not a standard library so the program is drawing a dependency from it. When the new artifact with the green + icon, specify that you want to create with dependencies instead of empty. I'll update the answer with another screen shot in a few to help.
  • MandisaW
    MandisaW about 4 years
    It's worth noting that as of Apr 2020, IDEA's own HelloWorld Java tutorial (jetbrains.com/help/idea/…) doesn't include the step of adding your compile-output to the target JAR. So thanks!
  • Andrea Gorrieri
    Andrea Gorrieri almost 3 years
    still can't add external dependencies even adding them with doble click as indicated :(