Skip to content

How to fix “Error: could not find or load main class” in Java?

  • by
  • 3 min read

Java is one of the most popular programming languages on the planet and has been for quite some time. It’s pretty easy to pick up for newcomers and pretty powerful in terms of what you can create using the language.

However, the JVM or the Java Virtual Machine doesn’t always work as expected, which means you’ll sometimes see errors that’ll refute all logic. In this article, we’re talking about the “main class not found” issue in Java and what you can do to try and fix the problem. 

Also read: Python vs Java vs C/C++: Key differences and Pros-Cons

What causes the “main class not found” error?

The error is pretty perplexing to newcomers, especially because every Java program has a main class, and the compiler reporting it doesn’t just doesn’t make sense.

However, the error is caused by JVM’s tendency to stick to a default classpath. A classpath being the file path where Java’s Runtime Environment searches for classes and other resource files it might need. 

Every time you run a Java program, it generates a .class file. This file is used to run the program eventually. However, if JVM can’t find this file, you’ll be seeing errors like this. 

Also read: Ubuntu software centre not loading: 5 Fixes

How to fix this error?

There are a few things you can do to get around this error. Here’s how.

Use the right class name

When running Java files, be sure to use the right class name; keep in mind that the .class file generated by your program will have the exact name as your class in the program itself since Java is case sensitive as well, you’re going to have to pay extra attention to get everything right. 

File extensions

In addition to the last point, make sure to use the right extensions when running a Java program. .java files are used when compiling Java programs, and .class files are used to run them.

How to fix "Error: could not find or load main class" in Java?
Having the right paths and class names is important.

Another thing to keep in mind is that when compiling, you have to use the name of the .java file and not necessarily the class name. However, .class files are always generated with the filename of the corresponding Java program, which means you need to get the class name right. 

Package names

Java usually groups similar classes into a package. When dealing with a class in a package, you need to make sure that you mention the full name of the package. 

For example, if you have a class CyberSec in package Candid.Technology, you’re going to have to mention it like CyberSec.Candid.Technology. Note that this is only an example to show how package nomenclature works. 

Another thing you need to make sure of is that you need to issue the command from the parent directory of the package and not the actual directory of the file, as then Java will search for the package in that particular directory.

Manually specify the ClassPath

As mentioned above, JVM sticks to default classpaths by default. While this may work in most cases, the classpath isn’t always accurate. You can manually specify the classpath using the -classpath variable. Here’s how. 

java -classpath [enter path here]

Using this variable, you can specify custom classpaths to precompiled files and hence get around the error.

Also read: How to install Java on Windows 10?

Yadullah Abidi

Yadullah Abidi

Yadullah is a Computer Science graduate who writes/edits/shoots/codes all things cybersecurity, gaming, and tech hardware. When he's not, he streams himself racing virtual cars. He's been writing and reporting on tech and cybersecurity with websites like Candid.Technology and MakeUseOf since 2018. You can contact him here: