Picture this: you’ve just written a simple Java program, compiled it, and you’re eagerly waiting to see it run. Instead, you get the dreaded message:
Error: Could not find or load main class MyMainClass
Ugh frustration sets in, and you’re stuck wondering: “What went wrong” I’ll guide you through how to fix “Error: Could Not Find or Load Main Class” in Java, using clear English, real world examples, and a complete coding project to practice with.
What the Error Actually
When you run:
java com.mycompany.MyApp
and you get:
Error: Could not find or load main class com.mycompany.MyApp
I tried to locate the class com.mycompany.MyApp, and either I couldn’t find the class file at all, or I found it but couldn’t load it (due to missing dependencies, wrong classpath, invalid package, or other issues).
- Finding means locating
MyApp.classin the directory structure / classpath. - Loading means reading that class file, linking dependencies, verifying things. If you fail at loading (for instance because a superclass is missing), you’ll still often see the same error.
So fixing this error means you must check: where is the class, Are you referencing it correctly? Does the classpath include where it lives, Are there dependencies missing.
Major Root Causes
Incorrect Class Name or Casing:
Java is case-sensitive. If your class is MyApp and you execute java myapp, the JVM complains.
Also, you don’t include .class in the java command, you just give the class name (or fully-qualified name).
Package/Directory Structure Mismatch:
If your file starts with package com.mycompany; but your directory structure does not reflect that (com/mycompany/MyApp.java), you’ll get the error. The compiled class must be in com/mycompany/MyApp.class relative to the classpath root.
Wrong Current Directory or Classpath:
If you launch from a directory different than the classpath root, the JVM can’t find your class. Similarly, if you override classpath and omit “.” (the current directory) or you omit required paths, you’ll see this.
Missing Dependencies or Loading Problems:
Even if the main class is found, if that class depends on another class that’s not found (e.g., a superclass or library), the load fails and you get the same message. This nuance is less covered in many articles.
Packaging and JAR Manifest Issues:
If you package into a JAR and then run java -jar myapp.jar, you need a valid Main-Class: entry in META-INF/MANIFEST.MF, and possibly a Class-Path: entry for other JARs. If these are missing or wrong, the JVM can’t find or load the main class.
IDE or Build-Tool Misconfiguration:
When using IDEs (Eclipse, IntelliJ) or build systems (Maven, Gradle), the runconfiguration might point to the wrong module, working directory, or classpath. This often causes the error but many blog posts skip it.
Module System (Java 9+) Effects:
In Java 9 and above, if using modules (module-info.java), you might need special commands (like java --module) or proper module-path. If you ignore these and just treat things as old classpath, you might hit the error.
A Complete Coding Project Walk through
Let’s build a simple project, cause the error, and fix it. This gives you hands-on experience.
Project Setup:
Project root: FixMainErrorDemo/
Structure:
FixMainErrorDemo/
src/
com/
example/
app/
AppRunner.java
AppRunner.java:
package com.example.app;
public class AppRunner {
public static void main(String[] args) {
System.out.println("AppRunner is running!");
}
}
Compile via Command Line:
Open terminal in FixMainErrorDemo/ and run:
javac -d bin src/com/example/app/AppRunner.java
This compiles to bin/com/example/app/AppRunner.class.
Then run:
java -cp bin com.example.app.AppRunner
It prints:
AppRunner is running!
Great.
Introduce an Error:
Now imagine we mistakenly compile and run from wrong directory or wrong class name. For example:
cd bin/com/example/app
java AppRunner
This yields:
Error: Could not find or load main class AppRunner
Because you’re in bin/com/example/app and the JVM expects the classpath root at bin, so it’s looking for com/example/app/AppRunner.class under that path but since you’re already in that folder, you’re pointing wrongly.
Fix That Error
Solution: Go back to project root and run:
java -cp bin com.example.app.AppRunner
Or run from bin directory as:
cd bin
java com.example.app.AppRunner
Packaging into JAR and Causing the Error:
Now package into JAR:
jar cvf AppDemo.jar -C bin .
Then run incorrectly:
java -jar AppDemo.jar
You might see:
Error: Could not find or load main class com.example.app.AppRunner
Because the JAR lacks META-INF/MANIFEST.MF with Main-Class: com.example.app.AppRunner.
Fix: Create manifest.txt with:
Main-Class: com.example.app.AppRunner
Then build:
jar cvfm AppDemo.jar manifest.txt -C bin .
Then run:
java -jar AppDemo.jar
Now it prints “AppRunner is running!”
Missing Dependency Scenario
Imagine AppRunner uses a helper class in another package or another JAR (e.g., com.example.lib.Helper). If that JAR isn’t included in the classpath or manifest Class-Path:, the JVM might still say “Could not find or load main class …” because it fails to load the main class (due to missing helper).
Fix: Include that JAR in classpath or manifest.
IDE Scenario:
In IntelliJ or Eclipse, you set up a project, but if your “Run Configuration” sets wrong “Working directory” or wrong “Main class” or missing module/classpath, you’ll see the error.
Fix: In IntelliJ Go to Run → Edit Configurations → Application → Main class = com.example.app.AppRunner, Use classpath module of project, Working directory = project root, ensure compiled output is included.
Debugging Workflow
- Ensure you compiled the class.
- Ensure
mainmethod signature is correct (public static void main(String[] args)). - Check class name spelling and casing.
- Check
packagedeclaration matches directory structure. - From project root, run
java -cp <outputDir> <fullyQualifiedClassName>. - If using JAR: check
META-INF/MANIFEST.MFcontainsMain-Class:and correctClass-Path:if needed. - If error persists: run
java -verbose:class -cp <…>orjava -Xdiag(in newer JDKs) to see class loading debug info. - If using IDE, verify run config: working dir, classpath, module, main class.
- Check dependencies: ensure any classes your main class references are present on classpath.
- For Java 9+ modules: check if you need
--moduleargument or if you compiled with modules.
Final Thoughts
If you ever ask yourself “How to Fix ‘Error: Could Not Find or Load Main Class’ in Java”, just remember: it’s usually nothing mystical it’s about incorrect class location, wrong classpath, packaging mistakes, or missing dependencies.
