Lỗi could not find or load main class trong cmd năm 2024

After running java Main, you get the error message, “Could not find or load main class”. What does this mean?

The Solution

This error message usually means that you gave the wrong file (or no file) to java on the command line.

If you run java thisfiledoesnotexist in your terminal or command prompt, you should see the same error.

Error: Could not find or load main class thisfiledoesnotexist Caused by: java.lang.ClassNotFoundException: thisfiledoesnotexist

A common cause of this error is passing in the .class file that javac creates by mistake. For example, let’s say you have the following code in a file called HelloWorld.java:

public class HelloWorld {

public static void main(String[] arg) {
    System.out.println("Hello");
}
}

When you run javac HelloWorld.java, the file

public class HelloWorld {

public static void main(String[] arg) {
    System.out.println("Hello");
}
}

0 is produced, which is the compiled version of the program that you can execute.

But to run this program, you should use:

Output:

If you run:

The output will be:

Error: Could not find or load main class HelloWorld.class Caused by: java.lang.ClassNotFoundException: HelloWorld.class

Some other common mistakes that might give you this error include:

  • Using the wrong case. For example, public class HelloWorld {
    public static void main(String[] arg) {  
        System.out.println("Hello");  
    }  
    
    } 1 instead of public class HelloWorld {
    public static void main(String[] arg) {  
        System.out.println("Hello");  
    }  
    
    } 2
  • Typing errors. For example public class HelloWorld {
    public static void main(String[] arg) {  
        System.out.println("Hello");  
    }  
    
    } 3 instead of public class HelloWorld {
    public static void main(String[] arg) {  
        System.out.println("Hello");  
    }  
    
    } 2
  • Using the incorrect directory. First use public class HelloWorld {
    public static void main(String[] arg) {  
        System.out.println("Hello");  
    }  
    
    } 5 to get to the same directory as your public class HelloWorld {
    public static void main(String[] arg) {  
        System.out.println("Hello");  
    }  
    
    } 6 and .class files, and then run public class HelloWorld {
    public static void main(String[] arg) {  
        System.out.println("Hello");  
    }  
    
    } 2.

Loved by over 4 million developers and more than 90,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

The Java error “Could not find or load main class” is thrown when the JVM fails to find or load the main class while executing a program. This is often due to simple mistakes like typing the wrong class name or having the class file in the wrong place. It usually occurs when executing a Java program from the command line.

Install the Rollbar Java SDK to identify and fix these errors

What Causes "Error: Could not find or load main class"

It typically arises from issues like an incorrect class name, a mismatch in the directory and package structure, or a misconfigured classpath. Here's a full list of things to check:

  • 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:

Say you have an example Java class MyClass.java:

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

You then compile the above class 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"

Most of the time, the error occurs because of specifying an incorrect class name, class file extension, file path or classpath.

Follow these tips to resolve it:

  • Use the correct class name - The spelling and casing of the class name should be checked when executing the program.
  • UseUse 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
  • Use 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 the 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

    $ javac MyClass.java

    0 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!