What is Java could not find or load main class?
The Error Could not find a class or load the main class that occurs when a program is running, but the main class cannot be found or loaded. In this article, we will learn the different reasons for the occurrence of the error and ways to rectify it.
What is java <class-name> syntax?
The java basic <class –name> is as follows:
java [ <option> ... ] <class-name> [<argument> ...]
- <option> is the command line option required during execution,
- <class-name> is the Java class’s name and
- <argument> is the command-line argument passed during the compilation of the program.
java -Xmx100m com.acme.example.FirstProg one two three
Here, the compiled version of the class name will be searched. Then the class will be loaded, and the main class will be searched for execution. If the statement public static void main(String args) is correct and the access modifiers, return types, and has the correct signature, the execution will be completed.
What are the Causes of the error ‘Could not find or load main class’ in Java?
The causes of the error ‘Could not find or load main class’ in Java are as follows:
- It is caused when a program is executed in the terminal, and the main class is not found or cannot be loaded.
- This is caused mainly due to syntax errors by the programmer.
- If the CLASSPATH environment variable where Java searches for all the class files does not have the main class, this error is shown.
- As Java is case sensitive, the incorrect casing for the main class can cause problems.
- If the class is inside a package, the main class will not be loaded.
- The incorrect directory is mentioned in the CLASSPATH.
How to Fix ‘Could not find or load main class’?
The methods to fix the error are:
1) Use Exact Classname instead .class file
You must execute the program by inserting the exact classname instead of the .class file. Therefore use the java classname For example, if the following syntax is compiled
There will be an error. Instead, we have to type
2) Check and Resolve Syntax Errors
- Reduce syntax errors by Check the classname syntax and case before declaring it. For example, if we write the syntax
And try to compile it, there will be an error. Therefore, type the syntax
3) Define Class-Path
- Check the classpath and reset it if the error shows up. You have to set the classpath to the current directory. For example, to set classpath in Windows, type the following syntax
CLASSPATH = %CLASSPATH%;.
The dot at the end represents the current directory.
4) Check Spelling and Case
- Check the spelling and casing of the classname while typing the command. For example, if we type, the error will be due to the incorrect spelling of the classname.
As Java is again a case sensitive, you also need to take care of casing.
5) Using .back Command
- If the class exists inside a package, then navigate back to the main directory through the
.back command. For example, the java class exists in the below path
The full name of the class will be com.projects.Program1. Then use the cd..back command to reach the parent directory primary
You can then run the java command to load the class through the following syntax.
6) Check Classpath Directory and Sub-Directory
- Check the directory and the sub-directory of the classpath. For example, the classpath is set to E:primarycomprojects, then make sure that you do not write E:primaryprojectscom.
We have observed the various reasons for the “Error Could not find a class or load main class” and it can be noted that the basic syntax errors are the root causes of such a problem. Keeping in mind the casing of the Java classes and proper classpath settings can prevent such errors in the future.