Blog |

Java Guide: How to Fix “Could not find or load main class”

Java Guide: How to Fix “Could not find or load main class”
Table of Contents

The Java “Could not find or load main class” error is thrown when the JVM fails to find or load the main class while executing a program. It usually occurs when executing a Java program from the command line.

Install the Java SDK to identify and fix these errors

What Causes Error: Could not find or load main class

The "Could not find or load main class" error occurs when the JVM fails to load the main class. This can happen due to various reasons, such as:

  • The class being declared in the incorrect package.
  • The file path of the class not matching the fully qualified name.
  • Incorrectly specified classpath of the application.
  • Missing dependencies from the classpath.
  • Incorrect directory path on the classpath.
  • A typo in the class name.

 

Error: Could not find or load main class Example

Here’s an example of the Java "Could not find or load main class" error thrown when an incorrect class name is specified during execution:

Here’s an example Java class MyClass.java:

public class MyClass {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

Now the above class is compiled using the command line:

$ javac MyClass.java

The compiler generates an executable .class file for MyClass:

$ ls
MyClass.class   MyClass.java

Now if the java command is used to execute the .class file with an incorrect name, the "Could not find or load main class" error is thrown:

$ java Myclass
Error: Could not find or load main class Myclass

The generated .class file has the exact same name as the Java class, which in this case is MyClass.class. Specifying the correct name will execute the program successfully:

$ java MyClass
Hello World

 

How to Fix Error: Could not find or load main class

There are several ways the "Could not find or load main class" error can occur while executing Java programs. Most of the time, it occurs because of specifying an incorrect class name, class file extension, file path or classpath.

The following tips can be useful to resolve the "Could not find or load main class" error:

  • Using correct class name - The spelling and casing of the class name should be checked when executing the program.
  • Using the class name without the .class extension - The java command expects the class name for executing the program, without the .class extension. Therefore, the following syntax should be used to execute Java classes: java <classname>
  • Using the correct file path - The path to the .class file should be checked and corrected if the error occurs. Remember to use the fully qualified name of the class that is in a package if executing it from outside the directory structure of the package.
  • Correct classpath definition - The classpath should be checked and defined correctly if the error comes up. It can also be specified using the java -cp or -classpath command line arguments.

 

Track, Analyze and Manage Errors With Rollbar

Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing Java errors easier than ever. Sign Up Today!

Related Posts

See all posts

"Rollbar allows us to go from alerting to impact analysis and resolution in a matter of minutes. Without it we would be flying blind."

Start continuously improving your code today.

Get Started Shape