Java Error: Could Not Find or Load Main Class? Fix It Now

Java Error: Could Not Find or Load Main Class? Fix It Now

When running a Java program, you might encounter the “Error: Could not find or load main class” message. This issue usually happens when Java cannot locate or execute the class that contains the primary method.

This article explains why this error occurs and how to fix it in different environments, including the command line and Eclipse. If you struggle with classpath errors, incorrect package structures, or execution mistakes, follow these steps to resolve the issue.

Why Does “Could Not Find or Load Main Class” Happen?

Why Does "Could Not Find or Load Main Class" Happen?This error occurs when Java fails to find the compiled .class file or cannot properly load it due to incorrect execution commands. Below are some common causes:

  • A mismatch between package declaration and file path – If a Java class is inside a package, but you run it without specifying the full package name, Java will not find it.
  • Incorrect file path – Running the Java program from the wrong directory prevents Java from locating the class.
  • Misconfigured classpath – If Java does not know where to look for class files, it will fail to load them.
  • Missing dependencies – If required .class files or libraries are missing from the classpath, execution will fail.
  • Typos or incorrect capitalization – Java is case-sensitive, so MyClass and myclass are treated as different files.

Example of the “Could Not Find or Load Main Class” Error

Imagine you create a simple Java program in a file named HelloJava.java:

public class HelloJava {
public static void main(String[] args) {
System.out.println(“Java is running!”);
}
}

Step 1: Compile the Java File

To compile the program, use this command:

javac HelloJava.java

If the compilation is successful, it creates a file named HelloJava.class in the same directory

Step 2: Running the Program with an Incorrect Class Name

If you mistakenly run the program using a lowercase letter:

java hellojava

Java will return this error:

Error: Could not find or load main class hellojava

Step 3: Running the Program with the Correct Class Name

Since Java is case-sensitive, the correct command must match the exact class name:

java HelloJava

Output:

Java is running!

Step 4: Running a Program with a Package Declaration

If your class is inside a package like this:

package mypackage;

public class HelloJava {
public static void main(String[] args) {
System.out.println(“Java is running!”);
}
}

You need to compile it from the parent directory of mypackage:

javac mypackage/HelloJava.java

Then, run it using the fully qualified name:

java mypackage.HelloJava

If you only use java HelloJavaJava will not find the class and will return an error.

Steps to Fix “Could Not Find or Load Main Class”

Steps to Fix "Could Not Find or Load Main Class"Step 1: Use the Correct Class Name

Ensure that the class name in your execution command matches the actual Java class name. Java is case-sensitive, so HelloJava is not the same as hellojava or HELLOJAVA.

Step 2: Do Not Include the .class Extension When Running Java

When executing Java programs, do not include the .class extension in the command.

  • Incorrect:
    java HelloJava.class

 

  • Correct:
    java HelloJava

 

Java automatically looks for the .class file, so including the extension will cause an error.

Step 3: Check the Package Declaration and Execution Path

If your Java file is inside a package, run it from the correct directory and use the fully qualified name.

Example: If your file is in mypackage/HelloJava.java with the package declaration:

package mypackage;

Then, compile it from the parent directory:

javac mypackage/HelloJava.java

And execute it using:

java mypackage.HelloJava

Running java HelloJava without the package name will fail.

Step 4: Run Java from the Correct Directory

Before running Java, navigate to the directory where the compiled .class file is stored.

If HelloJava.class is inside /home/user/projects/, navigate there using:

cd /home/user/projects/

Then execute the program:

java HelloJava

Running Java from the wrong directory prevents it from locating the class.

Step 5: Ensure the Classpath Is Set Correctly

If your program depends on other .class files or external libraries, you may need to specify the classpath.er/projects/, navigate there using:

  • To run a class from the current directory, use:
    java -cp . HelloJava
  • If your class is inside a package:
    java -cp . mypackage.HelloJava
  • If running a program that depends on a .jar file:
    java -cp mylibrary.jar HelloJava

A missing or incorrectly defined classpath is one of the most common causes of this error.

Step 6: Fix the Error in Eclipse

If you encounter this issue in Eclipse, follow these steps:

  • Go to Project > Clean and rebuild the project.
  • Check Run Configurations and verify that the correct class is the Main Class.
  • Ensure that the Java Build Path includes the correct directories.
  • If needed, delete the bin folder, restart Eclipse and rebuild the project.

Step 7: Recompile Your Java File

If your .class file is missing or outdated, Java will not find the class.

  • Delete old .class files:rm *.class
  • Recompile the Java file:javac HelloJava.java
  • Rerun it using:java HelloJava

This ensures Java is executing the latest compiled version of your program.

Final Thoughts

The “Could not find or load main class” error usually happens due to incorrect class names, package mismatches, misconfigured classpaths, or missing compiled files. Following the troubleshooting steps outlined above, you can quickly resolve the issue and successfully run your Java program.

If this guide was helpful, please share it with others. Also, be sure to check our Development section for more tutorials!