IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException


Error:

/Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home/bin/java 
-javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=53049:
/Applications/IntelliJ IDEA CE.app/Contents/bin -Dfile.encoding=UTF-8 Scratch

Error: Could not find or load main class HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld

Process finished with exit code 1
Class File:
public class HelloWorld {

    public static void main(String... args) {
        System.out.println("Hello");
    }
}

As you can see the above class does not have any complication issues or syntax errors. There can be multiple reasons that you may get the error, below are some of the solutions that may work for you!

  • Make sure that the class name is correct.
  • Go to Menu: Build -> Re-Build Project.
  • Remove .idea directory from your project and restart IDE.
  • Go to Files: Invalidate Cache/ Restart...
  • Check your project has a out folder, if not create one, example: /Users/code2care/IdeaProjects/java-examples/out
  • Edit Build/Run Configurations and check if you have selected the correct Java class that has a main method.

Fixing Could not find or load main class error
Fixing Could not find or load main class error



Recent Posts:




Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.