Read Also: Difference between PATH and CLASSPATH
"Could not find or load main class : XXX" where the class name is XXX occurs if the CLASSPATH environment variable where java looks for all class files, does not found the class.
[Fixed] Error : Could not find or load main class
1. Calling .class file from java command
public class HelloWorld { public static void main(String args[]) { System.out.println(" You have just run HelloWorld !"); } }
Suppose I have a simple HelloWorld java program. If I will compile it using command
javac HelloWorld.java
then HelloWorld.class file is created.
You will get the Error : Could not find or load main class if you try to run .class file using java command as shown below:
java HelloWorld.class
instead you should try
java HelloWorld
Yayy!! your problem is resolved.
You have just run HelloWorld !
2. If your casing incorrect
After compiling the code , you run the following commandjava helloworld
then also "Could not find or load main class helloworld" error appears. Make sure casing is correct. In our program it should be HelloWorld ( where 'H' of hello and 'W' of world is in uppercase).
java HelloWorld
Yayy ! It will work fine and print
You have just run HelloWorld !
3. Class in a package
In the below example I have a HelloWorld class inside the com.javahungry package.
package com.javahungry; /** * Java program to demonstrate * Error :Could not find or load main class * * @author Subham Mittal */ public class HelloWorld { public static void main(String args[]) { System.out.println(" You have just run HelloWorld !"); } }
If you try to call
java HelloWorld
It will result in Error : Could not find or load main class HelloWorld. The error occurs because it must be called with its fully qualified name. To be clear, the name of this class is not HelloWorld, It's com.javahungry.HelloWorld . Attempting to execute HelloWorld does not work, because no class having that name exists. Not on the current classpath anyway.
java com.javahungry.HelloWorld
Above command will also result in Error : Could not find or load main class HelloWorld because CLASSPATH environment variable is not set. I do not use -classpath or -cp command to suggest the path. By default java is searching the class file in the default directory.
java -cp . com.javahungry.HelloWorld
If your classpath is correct then above command will run the HelloWorld program. Otherwise, It will also result in Error : Could not find or load main class HelloWorld because -cp . command make sure the JVM looks for the class file in the current directory.
Follow below steps if error persist.
You must ensure that the location of the .class file is added in the CLASSPATH. Suppose if "/Users/SubhamMittal/Desktop/" is on the classpath and JVM looks for the class called "com.javahungry.HelloWorld", it will look for the .class file in the following path:
"/Users/SubhamMittal/Desktop/com/javahungry/HelloWorld"
java -cp /Users/SubhamMittal/Desktop/ com.javahungry.HelloWorld
Yayy ! It will print
You have just run HelloWorld !
The above example is performed on Mac OS.
4. Class in a Package for Windows OS
Suppose My java class i.e HelloWorld (given above) after compilation is in the below path
D:\project\com\javahungry\
The full name of the HelloWorld class is
com.javahungry.HelloWorld
so use cd .. back command to reach to the main directory
D:\project
Then issue the java command
java com.javahungry.HelloWorld
Yayy ! The program ran successfully without setting any classpath. It is because java is looking for the current directory denoted by .(dot) and able to locate the .\com\javahungry\HelloWorld.class .
It will print
You have just run HelloWorld !
5. Setting CLASSPATH to current directory
if .class file in the current folder then set . to your CLASSPATH (note in Windows the CLASSPATH separator is semi colon ; while in Linux the separator is colon :).
You can set the CLASSPATH in Windows :
set CLASSPATH = %CLASSPATH%;.(note . added at last for current directory)
You can set the CLASSPATH in Linux :
export CLASSPATH = ${CLASSPATH}:.(note . added at last for current directory)
If you are using java 1.5 or 1.6 , instead of getting error : could not find or load main class you will get Exception in thread "main" java.lang.NoClassDefFoundError : HelloWorld. Since jdk 1.7 we started receiving this new error. The good news, Solution is exactly same.
If you are not able to solve the problem by using above steps then please mention in the comments.