Java Fix method for main class not found or unable to load

  • 2021-08-12 02:38:30
  • OfStack

Sometimes, when we run the Java program, we may see "the main class cannot be found or loaded". The reason is easy to guess: JVM can't find the main class and gives this error. But why not?

In this article, we will discuss the possible reasons why the main class cannot be found. In addition, we will see how to fix them.

Sample program

We'll start with the HelloWorld program:


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

Now let's compile it:


$ javac HelloWorld.java

Here, the compiler will generate a. class file for our program. This. class file will be generated in the same directory. The class file will have the same name as the class name given in the Java program. This. class file is executable.

In the following section, we will run the. class file and try to understand the possible cause of the error "the main class cannot be found or loaded."

Wrong class name

To run the. class file generated by the Java compiler, use the following command:


java <.class filename>

Run our program:


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

Yes, it failed, and the mistake was “Could not find or load main class helloworld” That is, the main class helloworld cannot be found or loaded.

As mentioned earlier, the compiler generates the. class file with exactly the same name as the Java class in the program. So in our example, the name of the main class is HelloWorld, not helloworld, which is case sensitive.

Let's try again in the right way:


$ java HelloWorld
Hello world..!!!

This time it worked successfully.

File extension
To compile the Java program, you must provide a file name and its extension (. java):


$ javac HelloWorld.java

But to run the. class file, we need to provide the class name, not the file name. Therefore, there is no need to provide. class extension:


$ java HelloWorld.class
Error: Could not find or load main class HelloWorld.class

Again, let's run the program with the correct class name:


$ java HelloWorld 
Hello world..!!!

Java package name

In Java, we put similar classes in 1 and call them packages.

Let's move the HelloWorld class to com.baeldung Package:


package com.baeldung;

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

Now, let's compile and run the updated HelloWorld program as before:


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

But again we get the error "the main class HelloWorld cannot be found or loaded"!

Let's try to understand what we're missing.

To run the Java class in the package, you must provide its fully qualified name. So in our example, the fully qualified name of HelloWorld is: com.baeldung.HelloWorld .

Now, when we create com.baeldung Package, we actually created a folder structure, such as:


$ javac HelloWorld.java
0

First, let's try to start from com/baeldung Directory Runner:


$ javac HelloWorld.java
1

Nevertheless, we still can't run our program.

Here, when we specify a fully qualified class name: com.baeldung.HelloWorld , Java tried to find HelloWorld.class Class file in com/baeldung In the directory where we run the program.

Since we are already in com/baeldung, Java cannot find and run the HelloWorld program.

Now, let's go back to the parent folder and run it:


$ javac HelloWorld.java
2

Yes, we can say hello to the world again.

Invalid ClassPath

Before continuing, let's first look at what the ClassPath classpath is--it's the 1 set of classes available for the currently running JVM.

We use the classpath variable to tell JVM where to find the. class file on the file system.

When running the program, we can use the-classpath option to provide the classpath. Examples:


java -classpath /my_programs/compiled_classes HelloWorld

Here, Java will look for HelloWorld.class Class file in /my_programs/compiled_classes Folder, the name of this folder is just compiled by us. By default, the classpath variable is set to ".", which indicates the current directory.

In the above section, we changed the directory to run the program. But what if we want to run it from another folder? At this point, the classpath variable can help us.

To run our program from the com/baeldung directory, we can simply say that our ClassPath is two directories up--one for each package section:


$ javac HelloWorld.java
4

Here, "…" means the parent directory. In this example, "../../" represents the top of the package hierarchy.

The above is the Java can not find or can not load the main class of the repair method details, more information about Java can not find or can not load the main class please pay attention to other related articles on this site!


Related articles: