Java Error: Could Not Find or Load Main Class – Causes and Fixes

-

Java Error: Could Not Find or Load Main Class – Causes and Fixes

New Java programmers often encounter the cryptic message “Error: Could not find or load main class …” when running their programs. This error usually means that the Java Virtual Machine (JVM) cannot locate or load the class that contains the main method. In this article, we’ll explain what the message means, why it occurs, and how to fix it.

Quick Answer

The message “Could not find or load main class” indicates that the JVM couldn’t locate or load the class file you specified. Common causes include using the wrong class name or case, running the wrong file (like HelloWorld.class instead of HelloWorld), running a class from the wrong directory, using an incorrect package or classpath, or misconfiguring a JAR’s Main-Class. To fix it, compile your .java file, run it using the correct class name (without .class), ensure you are in the correct directory or set the classpath properly, and if the class is inside a package, run it with its fully qualified name.

What Causes the Error?

The JVM searches for the class to run based on the command you provide. If it can’t find the class or if the class doesn’t have a valid main method, it raises the error. Key causes include:

CauseExplanationSource
Wrong class name or caseThe class name you use with the java command must match the compiled class, including capitalization. Running java helloworld instead of java HelloWorld will trigger the error.Baeldung
Including file extensionsWhen running a class, don’t include the .class extension. Invoking java HelloWorld.class results in the error.Baeldung
Incorrect directoryThe JVM looks for the class in the current working directory by default. Running the java command from the wrong folder will cause the JVM to fail to load the class.Sentry
Package mismatchIf your class is inside a package (e.g., com.example.Main), you must use the fully qualified name and run from the parent directory of the package structure.Baeldung
Invalid classpathIf the class file resides in a different directory, you need to set the classpath using the -classpath (or -cp) option so the JVM knows where to find it.Baeldung
Missing Main-Class in a JARWhen running an executable JAR with java -jar app.jar, the JAR’s manifest must declare a Main-Class. Otherwise, the JVM can’t find which class to run.

Understanding these scenarios helps you choose the right fix.

How to Fix It

1. Use the Correct Class Name (Case Sensitive)

When you compile HelloWorld.java with javac HelloWorld.java, it produces HelloWorld.class. To run it, use java HelloWorld. The class name is case‑sensitive, so java helloworld will fail.

2. Don’t Include the .class Extension

Passing a .class file to the java command doesn’t work. The JVM expects a class name, not a filename. For example, java HelloWorld.class triggers the error. Always omit the extension.

3. Check Your Working Directory

By default, the JVM looks for the class file in the current directory. If you run java HelloWorld from a different folder, the JVM won’t find the class. First cd into the directory that contains your .class file or specify the correct classpath. The Sentry article lists running commands from the wrong directory as a common mistake.

4. Use the Fully Qualified Name for Packages

When your class belongs to a package, you must specify its fully qualified name. Suppose HelloWorld is declared in the com.baeldung package. Compile it with javac com/baeldung/HelloWorld.java. To run it from the root of your project, type java com.baeldung.HelloWorld. Running it from within the com/baeldung folder will fail because the JVM will look for com/baeldung/com/baeldung/HelloWorld.class.

5. Set the Classpath When Needed

If your compiled classes are in another directory, tell the JVM where to find them using the -classpath option. For example:

javac -d out src/com/example/Main.java
java -classpath out com.example.Main

Here, -d out directs javac to put compiled files in the out folder, and -classpath out tells the JVM where to look.

6. Verify the Manifest in JAR Files

When running a JAR with java -jar app.jar, the JAR’s manifest (META-INF/MANIFEST.MF) must specify Main-Class: com.example.Main. If the manifest is missing or incorrect, the JVM can’t find the main class. Use a tool like the jar command to view or edit the manifest:

jar tf app.jar              # list contents
jar xvf app.jar META-INF/MANIFEST.MF

If the Main-Class attribute is missing, rebuild the JAR with the correct manifest.

7. Avoid Typos and Path Separators in CLASSPATH

Typographical errors in class names or extra semicolons/colons in the CLASSPATH environment variable can lead to the error. Ensure there are no blank elements in the classpath and that each directory or JAR is separated properly.

Troubleshooting Checklist

  1. Confirm the class file exists – after compiling, verify that HelloWorld.class (or your class name) exists in the expected directory.
  2. Run with the correct name – use java YourClassName without .class and with proper capitalization.
  3. Navigate to the correct folder – use cd so that your working directory matches the classpath or specify -classpath explicitly.
  4. For packaged classes – call the class using its full package name (packageName.ClassName) from the project root.
  5. Adjust the classpath – pass -classpath or set the CLASSPATH environment variable to include directories or JARs containing your classes.
  6. Check JAR manifests – ensure your JAR file has a valid Main-Class attribute.

Conclusion

The “Could not find or load main class” error usually stems from mismatches between class names, directories, packages and the JVM’s classpath. By compiling with the correct filenames, running with the proper class name and capitalization, ensuring you’re in the right directory or using -classpath, and providing fully qualified names for packaged classes, you can quickly resolve this common issue. Taking a moment to understand how Java locates classes will save you time and frustration as you build more complex projects.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Recent comments