Error: Could not find or load main class error occurs if java could not find or load the main class, or if an error occurred when loading the main class. The main class could not be loaded because the java class specified in the java command could not be found. It’s possible that the java class isn’t available or that the path to it is incorrect. The Java class was not able to load into memory.

The Error: Could not find or load main class error means that the main class could not be found or loaded by the java command. This error happens when we try to run a Java program. The JVM displays the following error message Error: Could not find or load main class when the issue occurs. The error is mostly generated when we incorrectly specify the class name at run time of the program. The issue is most commonly caused by mistakenly specifying the class name during java program execution.



Example

The following example java program will print a string to the console. The java program is compiled using the javac compiler. When the java program is run with the java command, the error will be displayed.

package com.yawintutor;

public class HelloWorld {
	public static void main1(String[] args) {
		System.out.println("Welcome to Yawin Tutor");
	}
}
javac com.yawintutor.HelloWorld.java

java com.yawintutor.HelloWorld.java 

Output

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


Solution 1

The java class is identified by the java extension. The java file name with extension should be given in the javac command when the java class is compiled. The extension should not be supplied while running the java command.

//invalid
java com.yawintutor.HelloWorld.java 

// correct 
java com.yawintutor.HelloWorld


Solution 2

It’s possible that the java class or package name is misspelt. Correct any spelling problems that you find. In the java command, the right java class name and path should be specified.

//invalid
java com.yawintutoor.HelloWorld 

// correct 
java com.yawintutor.HelloWorld


Solution 3

The name of the Java class is case sensitive. The java command could not find the class if the case of the java class name or package name was incorrect. The error is going to be thrown.

//invalid
java com.yawintutor.helloworld 

// correct 
java com.yawintutor.HelloWorld


Solution 4

In the java command, the package name is not mentioned in the java class name. To find the java class, you’ll need to have the package name. Java was unable to locate the class if the package name was not given.

//invalid
java HelloWorld 

// correct 
java com.yawintutor.HelloWorld


Solution 5

The name of the Java package should be supplied in the java command. The error will be thrown if the file path or path name is specified in the command. The file path should be changed to the name of the Java package.

//invalid
java com/yawintutor/HelloWorld 

// correct 
java com.yawintutor.HelloWorld


Solution 6

The java command is executed without the java class being created. The java class will be created with the help of the java compiler. To create a java class file, use the javac command. Before executing the java command, this should be executed.

//invalid
java com.yawintutor.HelloWorld 

// correct 
javac com.yawintutor.HelloWord.java
java com.yawintutor.HelloWorld


Solution 7

The java command is run without supplying the name of the java class. The java command line options have been added, however the java class name has been missed.

//invalid
java -src /usr/local/test -d ./target 

// correct 
java -src /usr/local/test -d ./target com.yawintutor.HelloWorld


Solution 8

It’s possible that the java class path refers to other directories. The class path may or may not include the current directory. To run the Java class, the current path should be added.

//invalid
java com.yawintutor.HelloWorld 

// correct 
java -cp . com.yawintutor.HelloWorld


Solution 9

The java class could be created in a directory structure that is not the same as the package name. The package name specified in the java file will be used to create the java class. It’s possible that you’re running the package with the same name as the file path.

src/com/yawintutor/util/HelloWorld.java

package com.yawintutor;

public class HelloWorld {
	public static void main1(String[] args) {
		System.out.println("Welcome to Yawin Tutor");
	}
}
//invalid
java com.yawintutor.util.HelloWorld 

// correct 
java com.yawintutor.HelloWorld


Solution 10

The java class may be created in the different directory structure in the project structure. For example, the java class is created under the test folder. You are trying to execute the class in the src directory.

src
 - main
   - java
     - com.yawintutor
 -test
   - java
     - com.yawintutor
        - HelloWorld.java
//invalid
src/main/java> java com.yawintutor.HelloWorld 

// correct 
src/test/java> java com.yawintutor.HelloWorld


Solution 11

The java class may have dependency jars to execute. If the dependent jars are not added in the class path, the java could not load the main class.

//invalid
java com.yawintutor.HelloWorld 

// correct 
java -cp .:./lib/log4j.jar com.yawintutor.HelloWorld



Leave a Reply