Resolving 'Could Not Find or Load Main Class' Error in Java

JavaJavaBeginner
Practice Now

Introduction

Errors and exceptions are common in programming, including Java. One of the common errors developers encounter is the "Could Not Find or Load Main Class". This error simply means that the Java Virtual Machine (JVM) cannot find or load the specified main class. In this lab, we will look at the reasons behind this error and provide solutions for resolving this error in Java.

Setting up the Project

Create a source code file called ErrorDemo.java in the ~/project directory with the following code:

public class ErrorDemo {
    public static void main(String[] args) {
        System.out.println("Error Fixed");
    }
}

Compiling and Running the Program

Compile the source code file using the javac command in the terminal:

javac ErrorDemo.java

Running the Program

This command generates a .class file with the same name as the main class.

Run the compiled program using the java command:

java ErrorDemo

This command should print "Error Fixed" to the terminal.

Incorrect Class File Name

Deliberately make a mistake by renaming the .class file to something else, such as Error.class. Then run the program using the following command:

java Error

This should produce the following error message:

Error: Could not find or load main class Error

The error occurs because the JVM cannot find the main class.

Incorrect Directory

Navigate to a different directory than the one containing the .class file. Then try to compile and run the program again using the following commands:

javac ~/documents/ErrorDemo.java
java ErrorDemo

This should produce the following error message:

Error: Could not find or load main class ErrorDemo

The error occurs because the Java Virtual Machine is looking for the main class in the current directory.

Incorrect Package Name

Add a package name to the code and move the code file into a directory named my_package. The ErrorDemo.java file now looks like this:

package my_package;

public class ErrorDemo {
    public static void main(String[] args) {
        System.out.println("Error Fixed");
    }
}

Compile the .java file again using the javac command. Then try running the program using the following command:

java ErrorDemo

This should produce the following error message:

Error: Could not find or load main class ErrorDemo

The error occurs because the Java Virtual Machine is now looking for the main class in the my_package directory.

Correct Package Name

Try running the program again using the fully qualified class name, including the package name, using the following command:

java my_package.ErrorDemo

This should print "Error Fixed" to the terminal.

Using Classpath

Create a new directory named classes, and move the ErrorDemo.class file into this new directory.

mkdir classes
mv ErrorDemo.class classes/

From the ~/project directory, try running the program using the java command and the -classpath flag to specify the location of the .class file:

java -classpath classes ErrorDemo

This should also print "Error Fixed" to the terminal.

Summary

The "Could Not Find or Load Main Class" error in Java is usually caused by incorrect naming conventions or incorrect package names. We can resolve this error by ensuring that the class name is correct, the package name is correct, and running the program in the correct directory. Additionally, we can use the -classpath flag to specify the location of the .class file and resolve the error.

In this lab, we have covered the reasons behind the "Could Not Find or Load Main Class" error and how to fix them. By ensuring the correct naming conventions, package names, and directories, we can compile and run our Java programs without encountering this error.

Other Java Tutorials you may like