If you are getting Error: Could not find or load main class
error, it means JVM is trying to load a class with main method. Simply, JVM cannot find this class in the classpath.
main method
main() method has a special meaning in Java, it is the entry point of Java programs.
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } }
When you run the following command, it starts JVM, loads HelloWorld class and starts running its main method.
java HelloWorld
We will go through several possible reasons for this error. We usually use IDEs for software development, so first let’s examine how to fix this problem in our IDEs.
IntelliJ IDEA – Solution 1
If you run a main class through you IDE, it will be stored in Run/debug Configurations. You can easily run the class again with the run/debug buttons at a later time.


You might want to change the name of your class (Main -> Main2). If you update a class name on your IntelliJ, all references to this class will also be updated (including Run/debug Configurations). However, if you change a class name (or change its package) outside your IDE, Run/debug Configurations will become obsolete.
I renamed Main.java to Main2.java outside Intellij IDE and tried to run the code again using Run button. Now, it gives the following error message in console.
C:\Java.jdks\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 211.6556.6\lib\idea_rt.jar=14581:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 211.6556.6\bin" -Dfile.encoding=UTF-8 -classpath C:\abcstudyguide\codes\out\production\Main com.abc.Main
Error: Could not find or load main class com.abc.Main
Caused by: java.lang.ClassNotFoundException: com.abc.Main
Process finished with exit code 1
You can update Run/debug Configurations to fix the error.


IntelliJ IDEA – Solution 2
Sometimes just rebuilding your project is enough to resolve this error. To rebuild the project, select Build -> Rebuild Project.

IntelliJ IDEA – Solution 3
If there is some residue left in the cache, you can invalidate caches and restart you IDE.

Eclipse – Solution 1
Similar to IntelliJ, you can run Java application in eclipse.

Once you run your application, it is saved as a Run Configuration. If you update your application main class outside Eclipse IDE and try to run it again, you will get Error: Could not find or load main class
error message.

If you have updated class name or package outside Eclipse IDE, you need to update Run Configurations accordingly.

Eclipse – Solution 2
If there is nothing wrong with your Run Configurations, you can try to clean and build your project. Note that Build Automatically option is selected.

Eclipse – Solution 3
If you copied the project from another computer, your build path might be pointing incorrect path. To check the build path of your project, select File -> Properties from menu and open Java Build Path.
Eclipse warns you with message: Build path entry is missing: C:/jar_libs/jdbc.jar. To fix the path of jar file, click on Edit button and update with correct path.

Command Line – Solution 1
You can also use command line to compile and run your Java programs. Consider the following HelloWorld class. It is a simple class with a main method. There is no package declaration in the source code.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
You can compile your source code with javac
command and run the compiled code with java
command. There are a few things you need to pay attention to while running the code.
Java is case-sensitive, so you should type your class name exactly the same. If you type helloworld instead of HelloWorld, command will give error.
Also, file extensions (like HelloWorld.class) should not be used when running java command.
C:\codes>javac HelloWorld.java C:\codes>java HelloWorld Hello World! C:\codes>java helloworld Error: Could not find or load main class helloworld C:\codes>java HelloWorld.class Error: Could not find or load main class HelloWorld.class
Command Line – Solution 2
Following code is similar to previous one, but in this example HelloWorld class is defined under a package. In Java, packages are used to organize similar classes together.
Packages are organized as directories on your file system. For instance, HelloWorld class is created under com.abc.study.guide package, so HelloWorld.java file should be in {project-root}/com/abc/study/guide folder.
package com.abc.study.guide;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
To run a class, we are using FQN (Fully Qualified Name) of the class – that is (package.class name). For HelloWorld class in our example FQN is com.abc.study.guide.HelloWorld. Thus, java comand to run this class is: java com.abc.study.guide.HelloWorld
.
C:\codes>javac com\abc\study\guide\HelloWorld.java
C:\codes>java HelloWorld
Error: Could not find or load main class HelloWorld
C:\codes>java com.abc.study.guide.HelloWorld
Hello World!
You can run HelloWorld class from any directory in file system. But if you are running java command from outside the project root directory, classpath parameter should be used. Classpath tells JVM where to look for classes – it’s path of classes. You can specify the class path is by using the -classpath (or -cp) command line switch.
C:\another-directory>java com.abc.study.guide.HelloWorld Error: Could not find or load main class com.abc.study.guide.HelloWorld C:\another-directory>java -classpath C:\codes com.abc.study.guide.HelloWorld Hello World! C:\another-directory>java -cp C:\codes com.abc.study.guide.HelloWorld Hello World! C:\codes\com\abc\study\guide>java com.abc.study.guide.HelloWorld Error: Could not find or load main class com.abc.study.guide.HelloWorld C:\codes\com\abc\study\guide>java -cp ../../../.. com.abc.study.guide.HelloWorld Hello World!