The main() method is required to run/execute programs developed in the Java programming language since it is where the program execution begins. When starting a Java program, you could encounter the warning “error: Could not find or load main class.” You’re having this problem because you’re using the java command to run main() from within the class.
Note: You can also learn Errors and Exception in Python.
Table of Contents
How to fix could not find or load the main class?
There are many ways to solve this issue depending on the reason of occurring this error. We will discuss each reason one by one and try to fix this problem.
What are the possible causes or reasons of this error?
There are several reasons for this problem, which are listed below.
- File Extension
- Wrong Package
- Classpath is not valid
- The class name is incorrect
File Extension
We need to save the Java source code file with the extension .java to compile it. To compile a Java program, Java Compiler is being used as (javac command). After compilation, the .java file will be converted to a .class file.
As a result, your source code file will end in.java, while the produced file will end in .class. For compiling source code, we were using filename, but for running a compiled file, we cannot use the file name but the class name. Else it will throw an error like in the below example.
Example: HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Compiling HelloWorld Program

Running/Executing HelloWorld with the filename.

Running/Executing HelloWorld with the class name.

Wrong Package
Packages used in Java for group-related classes to write better maintainable code. To avoid name conflicts in group classes, we can use packages in Java programming. To launch a Java class in a package, we must use packageName with a fully qualified className. We cannot run it directly using the class name as in the previous example, and if we do so, we will get the error.
Example: HelloWorld.java
Package com.baeldung;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Compiling HelloWorld Program

Running/Executing HelloWorld without package name.

Running/Executing HelloWorld with the class name.

It is still showing an error because it could not find the HelloWorld file inside com/baeldung. We need to move back to the parent directory and rerun it.

Classpath is not valid.
The Java Virtual Machine searches the classpath for user-defined classes, packages, and resources in Java programs. If you correctly stated the class name but still received the same error, the Java command likely could not locate the supplied class name at the location. As a result, you must first confirm that the location of your .class file is included in your classpath.
Example: HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Compiling HelloWorld Program

Running/Executing HelloWorld at the default location.

Running/Executing HelloWorld at the location where the file exists.

The class name is incorrect.
This problem can occur if the name of your Java file (.java) and the primary class name is different. For the example, we have done class name HelloWorld, and the file name is HelloWorld.java. Let change the class name and execute the program.
Example: HelloWorld.java
public class helloworld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Compiling HelloWorld Program

Conclusion
In this article, we tried to resolve a Java error that could not find or load the main class differently. We discussed some reasons for this error and, depending on those reasons, applied the methods to fix the error. Also, we performed coding examples with executions with error-occurring conditions and solved the error accordingly.