Signup/Sign In

Could Not Find or Load Main Class Error in Java

Errors and exceptions are very common while working with Java or any other programming language. The could not find or load main class error is very common and can occur frequently. This error simply means that the JVM is unable to find the main class. Let's look at the reasons why this error occurs and try to solve this error.

Here, we have the following code in a java file called

public class ErrorDemo
	public static void main(String[] args)
		System.out.println("Error Fixed");

We can compile this code by using the javac command and provide the java file name.

Compiling the java code

The above command generates a .class file that has the same name as the class with the main method.

Viewing the class file

We can run the code and view the output by using the java command. We can see that everything is working fine, and we get the expected output.

Viewing the output of our program

Error When Incorrect Class File Name

Let's deliberately mess things up to view the could not find or load main class error. This error can occur if we try to run a java program but do not pass the correct .class file name.

Incorrect .class file name

Another thing to remember is that we use the javac command to compile a java file, and we need to add the .java extension to the file name when using it. But when running the .class file, do not pass the .class extension with the class name. This will also return the same error.

Adding the .class extension to the class name

Error When Incorrect Directory

Note that we must run the java command for the .class file in the correct folder. For example, if we navigate to the desktop(using cd .. command), then again, we will get this same error.

Incorrect directory

Error Incorrect Package Names

A package in Java is a set of similar classes or interfaces that are grouped together for easy access. When trying to run a .class file that is present in a package, we need to use the package name along with the class name.

Let's alter our program so that the class is included in a package.

package p;

public class ErrorDemo
	public static void main(String[] args)
		System.out.println("Error Fixed");

Now, when we run the javac command with the -d and a dot(.) then the .class file will be created in the package p.

Compiling the java file

Viewing the class file in the new package

Let's try to run this class file as we did in the above examples. But we get an error now.

Getting the could not find main class error

To rectify it, we also need to pass the package name while running the file.

Running the class file using fully qualified file name

Remember that we are running the above command from the parent directory and not from the package directory p.


As seen above, this error can also occur if we are running the .class file from a directory that does not contain this file. This is because, by default, the JVM will search for the class file in our current directory. A classpath informs the JVM to look for the file in a particular folder. Defining a classpath helps us to run a java class file from some other directory. Use the -classpath or -cp option with the java command to pass the classpath when running the .class file.

For example, if our .class file is present in a folder called errors and we are trying to run the code from the desktop, then we will get the following error.

Error because of wrong directory

We can use the classpath to rectify this error.

Mentioning the class path to rectify the error

Frequently Asked Questions

How do we run a file in Java?

As shown at the beginning of this article, a Java file is first compiled by using the javac command, and this generates a .class file. Next, we can run this .class file by using the java command. Read more.

How is path different from classpath?

The path is the location where we can find the executable file with an extension like .exe or .jar, and classpath is the location where we can find the .class files. By default, the classpath is set to the current directory where we are working.

What is ClassNotFoundException?

The ClassNotFoundException occurs when the JVM cannot find the class that we are trying to run in the mentioned classpath.

What to do when we get the following error after running the javac command: javac is not recognized as an internal and external command?

This error occurs because we have not set the PATH environment variable. We can set it temporarily by using the following command. Pass the path of the bin folder of your JDK.

C:\Users\user1>set path=C:\Program Files\Java\jdk1.8.0_144\bin

Read more about the Java environment setup.


The could not find or load main class error is very common in Java. Most of the time, this occurs because we are writing the class name incorrectly or we are adding unnecessary .class extensions while running. If the class is part of a package, then we must provide the fully qualified class name when running the code to avoid errors.

About the author:
I am a 3rd-year Computer Science Engineering student at Vellore Institute of Technology. I like to play around with new technologies and love to code.