DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 963,864 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for HOW TO FIX β€œERROR: COULD NOT FIND OR LOAD MAIN CLASS” IN JAVA?
Sakshii
Sakshii

Posted on • Originally published at codeleaks.io

HOW TO FIX β€œERROR: COULD NOT FIND OR LOAD MAIN CLASS” IN JAVA?

The main() method is required to run/execute programs developed in the Java programming language since it is where the program execution begins. When starting a Java program, you could encounter the warning β€œerror: Could not find or load main class.” You’re having this problem because you’re using the java command to run main() from within the class.

Note: You can also learn Errors and Exception in Python.

How to fix could not find or load the main class?
There are many ways to solve this issue depending on the reason of occurring this error. We will discuss each reason one by one and try to fix this problem.

What are the possible causes or reasons of this error?
There are several reasons for this problem, which are listed below.

File Extension
Wrong Package
Classpath is not valid
The class name is incorrect

File Extension
We need to save the Java source code file with the extension .java to compile it. To compile a Java program, Java Compiler is being used as (javac command). After compilation, the .java file will be converted to a .class file.

As a result, your source code file will end in.java, while the produced file will end in .class. For compiling source code, we were using filename, but for running a compiled file, we cannot use the file name but the class name. Else it will throw an error like in the below example.

Example: HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

Top comments (0)

Update Your DEV Experience Level:

Settings

Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. πŸ›