Could not find or load main class in Java


JavaViews 3675

In some situations, while executing a Java program from the command prompt, we may face the error “Could not find or load main class”. This occurs mainly when the JVM fails to find the main class or .class file. Whenever we compile a Java code, the compiler will automatically create a .class file with the same name as the class name. This will be present in the same directory where we have the .java file.

Could not find or load main class in Java

Let us see a simple Java code and try to compile and execute it from the command prompt.

public class HelloJava {

  public static void main(String[] args) {
    System.out.println("Java programming language");

  }

}

First, we need to compile the code using the javac command.

D:\Sample>javac HelloJava.java

This will produce the right result as below since we have compiled it correctly.

D:\Sample>java HelloJava
Java programming language

Now let us see the different reasons or causes to generate the error “Could not load or find the main class in Java“.

Could not find or load main class in Java

Could not find or load main class in Java: Incorrect class name

Whenever we compile the java code, the compiler creates a .class file in the same name as the class name. After compilation, if we try to run the code by providing an incorrect file name in the java command, it will result in an error “Could not load or find the main class in Java“.

D:\Sample>java hellojava
Error: Could not find or load main class hellojava
Caused by: java.lang.NoClassDefFoundError: HelloJava (wrong name: hellojava)

Since the class names “HelloJava” and “hellojava” are different, it could not find the right file to execute. The Java file names are case-sensitive during execution.

Could not find or load main class in Java: Incorrect file extension

During compilation, we provide the file extension as.java. In this case, it compiles and successfully generates the .class file. But for executing a java code, we do not need to provide the .class extension. In case we provide the .class as file extension, it will generate the below error “Could not find or load main class in Java”.

D:\Sample>java HelloJava.class
Error: Could not find or load main class HelloJava.class
Caused by: java.lang.ClassNotFoundException: HelloJava.class

If we try to execute without the file extension, it will execute successfully.

D:\Sample>java HelloJava
Java programming language

Package names

When we have multiple classes that we want to group together, we can group them under a single package. When we place a Java file inside a package, we need to specify the fully qualified name. This means we need to execute the file as packagename.filename.

Now, let us see how to execute the java code when we place the HelloJava.java file inside the package “javaDemo“. In the below example, we are already within the directory “javaDemo“. Hence the compiler tries to find the class file javaDemo.HelloJava and fails to find it. This will result in an error “Could not find or load main class in Java”.

D:\Sample\javaDemo>java javaDemo.HelloJava
Error: Could not find or load main class javaDemo.HelloJava
Caused by: java.lang.ClassNotFoundException: javaDemo.HelloJava

Suppose we move one directory up. Now, it runs successfully. Whenever we execute a java file with a fully qualified name, we need to move one directory up.

D:\Sample> java javaDemo.HelloJava
Java programming language

Could not find or load main class in Java: Invalid classpath

A classpath is a path that instructs the JVM where the .class files are present. We can also execute a Java file by specifying the classpath using –cp or -classpath option in the java command.

We can specify the classpath after the -cp command as below. Here HelloJava is present inside the directory javaDemo.

C:\Users\nandi\eclipse-workspace\FirstJavaProject\src>java -cp ./javaDemo;. HelloJava
Java programming language

If we try to specify the classpath when we are already within the target directory, we will get the below error “Could not find or load main class in Java”. This is because it searching for the package javaDemo which is not present since we are already within the same directory.

C:\Users\nandi\eclipse-workspace\FirstJavaProject\src\javaDemo>java -cp ./javaDemo;. HelloJava
Error: Could not find or load main class HelloJava
Caused by: java.lang.ClassNotFoundException: HelloJava

Reference

Translate »