interest

How to Solve “Could not find or load main class” Error?

This error occurs when a programmer tries to run the Java program by using a command with the main class as “HelloWorld” but Java could not find that class. You can resolve this error and run the program without any trouble. It requires you to learn how Java finds and loads classes.

Beginners may find it a bit daunting because that’s a bit complex topic. It takes some time and practice to master error solving, but here you will learn how to do it.

Java’s environment variable CLASSPATH contains directories in which Java searches for all the class files. Suppose Java can’t find the class file in that directory, it will show “Could not find or load main class XX” error. The class name will be mentioned at the spot of XX.

Follow the below-given steps to solve this error:                public class HelloWorld {
               
                    public static void main(String args[]) {
                        System.out.println(" You have just run HelloWorld !");      
                    }
                }
For example, if I have created HelloWorld java program, I will use command to compile this program.

                                  javac HelloWorld.java


Now HelloWorld.class file is created.

It will show the Error: Could not find or load main class if you run the class file using the below given java comman:

                                  java HelloWorld.class

So, you should try the below given command

                                  java HelloWorld

The issue is solved and you have run the program

                                  You have just run HelloWorld !Once the code is compiled, try the following command

                                  java helloworld

Suppose the same error pops up on the screen, then you have to check casing. Make sure you are using correct casing. In the above used program, the “h” and “w” letters are in small. Replace that command with HelloWorld command. Keep “H” and “W” letters in upper casing.
                                 java HelloWorld

The job is done and no print:

                                You have just run HelloWorld !Check the below illustrated example in which a “HelloWorld” class is 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 !");      
    }
}
Suppose you call “java HelloWorld”, it will pop-up Error : Could not find or load main class HelloWorld.

The error is appearing on your screen because you are not using its fully qualified name. You cannot use HelloWorld name here because class’s name is com.javahungry.HelloWorld.

It is showing could not find class error because the class name is missing. If you call

                      java  com.javahungry.HelloWorld

It will again show the same error because you have not set the CLASSPATH environment variable. Java is already searching for the class file in its default dictionary. Therefore, -classpath or –cp is not used here.

                     java -cp . com.javahungry.HelloWorld

Suppose the classpath is correct, the above-given command should run your program now. If not, the error will pop-up again. This time the error will come because –cp . command directs JVM to find the required file in the current directory. 

You can follow the below-give steps to resolve the error:

Add the location of .class file in the CLASSPATH. For example, if "/Users/SubhamMittal/Desktop/" location is on the Classpath and JVM searchs for the "com.javahungry.HelloWorld" class, it will try to find the .class file in the below given path.
"/Users/SubhamMittal/Desktop/com/javahungry/HelloWorld"

                  java -cp  /Users/SubhamMittal/Desktop/  com.javahungry.HelloWorld

The program is executed and it will print

                   You have just run HelloWorld !