Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 4535

Could Not Find or Load Main Class Error

$
0
0

1. Introduction

Occasionally when we run a Java program, we might see “Could not find or load main class”. It's easy to guess the reason: The JVM failed to find the main class and gave this error. But why couldn't it?

In this tutorial, we'll discuss the probable reasons for failure to find the main class. Also, we'll see how to fix them.

2. Sample Program

We'll start with a 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 sections, we'll run this .class file and try to understand the probable reasons for error “Could not find or load main class”.

3. Wrong Class Name

To run a .class file generated by Java compiler, we can use below command:

java <.class filename>

Now, let's run our program:

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

Yeah, it failed with the error “Could not find or load main class helloworld”.

As discussed earlier, the compiler will generate the .class file with the exact same name given to the Java class in the program. So in our case, the main class will have the name HelloWorld, not helloworld.

Let's give it one more try with correct casing:

$ java HelloWorld
Hello world..!!!

This time it ran successfully.

3.1. File Extension

To compile a Java program, we must provide the file name with its extension (.java):

$ javac HelloWorld.java

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

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

So, again, let's run our program using the correct class name:

$ java HelloWorld 
Hello world..!!!

4. Java Package Names

In Java, we keep similar classes together in what we call a package.

Let's move HelloWorld class into the 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 like before:

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

But again we get the error “Could not find or load main class HelloWorld”!

Let's try to understand what did we missed here.

To run a Java class which is in a package, we must provide its fully qualified name. So in our case, HelloWorld's fully qualified name is com.baeldung.HelloWorld.

Now, when we created com.baeldung package, we actually created a folder structure like:

com/baeldung/HelloWorld.java

First, let's try to run our program from the com/baeldung directory:

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

Still, we are not able to run our program.

Here, when we specified the fully qualified class name com.baeldung.HelloWorld, Java tried to find HelloWorld.class file in com/baeldung, under the directory from where we were running the program.

As we were already inside com/baeldung, Java failed to find and run the HelloWorld program.

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

$ java com.baeldung.HelloWorld
Hello world..!!!

Yes, we are again able to say “Hello” to the world.

5. Invalid Classpath

Before going ahead, let's first understand what is the classpath – it's the set of classes available to our currently-running JVM.

We use the classpath variable to tell the JVM where to find the .class files on the filesystem.

While running a program, we can provide the classpath using -classpath option. example:

java -classpath /my_programs/compiled_classes HelloWorld

Here, Java will look for HelloWorld.class file in /my_programs/compiled_classes folder, a folder whose name we just made up. By default, the classpath variable is set to “.”, meaning the current directory.

In the above section, we changed our directory to run our program. But what if we want to run it from some other folder? That's when the classpath variable helps us.

To run our program from the directory is com/baeldung we can simply state that our classpath is two directories up – one for each package part:

$ java -claspath ../../ com.baeldung.HelloWorld
Hello world..!!!

Here, “..” represents the parent directory. In our case “../../” represents the top of our package hierarchy.

6. Conclusion

In this tutorial, we learned the probable reasons for the error “Could not find or load main class”. Then, we also learned to solve this error.


Viewing all articles
Browse latest Browse all 4535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>