Java: “Error: Could not find or load main class”

The JVM went looking for the class you named and its main method, and couldn’t find the class at all. This is the first error every Java student meets, and it’s nearly always about names and places: the class name doesn’t match the file, you’re in the wrong folder, the class is in a package you didn’t account for — or there’s no compiled .class file because javac never ran (or failed).

The fix, in order

  1. Compile first, and read the result: javac Main.java must complete without errors and produce Main.class in the same folder. Running java Main on a file that never compiled is the #1 cause.
  2. Run the class name, not the file name: java Main — not java Main.java and not java Main.class.
  3. Match the case exactly. The class name in the java command must match the public class name in the file, letter for letter — main and Main are different classes to the JVM.
  4. If the file declares a package (a package line at the top), run it by its full name from the parent of the package folders: java com.example.Main, executed from the directory that contains the com folder.
  5. Check the classpath if you use one: with -cp, the folder containing the class must be listed — java -cp . Main includes the current folder. A classpath that omits . loses local classes.

If it still happens

In an IDE, this error at launch usually means the run configuration points at the wrong main class, or the project didn’t build — check the build output before the code. And confirm the class truly has a main method with that exact signature (public static void main(String[] args)) — a near-miss (missing static) loads the class but rejects the method with a different message.

Common questions

Why did it work yesterday in the same folder? Usually the .class file was deleted, or the code was edited and recompiled with an error that scrolled past. Recompile and actually read javac’s output.

What’s the difference between this and ClassNotFoundException? Same family: the launcher prints “Could not find or load main class” as the headline, with ClassNotFoundException as the cause underneath. The checks are identical.

Related errors

Could not create the Java Virtual Machine · A JNI error has occurred · Python: NameError is not defined