Error: Could Not Find or Load Main Class in Java – Causes and Fixes
-
Last Updated: October 24, 2026
-
By: javahandson
-
Series

Fix “Error: Could not find or load main class” in Java fast. Causes — classpath, package, capitalization, working directory, JAR — with fixes and how to verify.
You run your program and get this instead of output: Error: Could not find or load main class. The could not find or load main class error almost never means your code is broken. It means the JVM is looking in the wrong place, or the class name you typed does not match reality.
⚡ Quick fix (works for most cases): If your class starts with a package line, compile and run like this — from the folder above your source: javac -d out src/com/app/Main.java java -cp out com.app.Main Rule of thumb: classpath = the root folder (the one holding com), class name = dotted, with no .java or .class extension.
If that did not solve it, the table below points you straight to your case. Then scroll to the matching fix.
| What you see / did | Real cause | Quick fix |
|---|---|---|
| Class has a package line, you ran the short name | Wrong class name | Run the full dotted name: java com.app.Main |
| java Main.class or java Main.java | Extension in command | Drop it: java Main |
| java main (lowercase) | Wrong capitalization | Match the class exactly: java Main |
| -cp points at the folder holding the .class | Wrong classpath root | Point at the package root, not the leaf folder |
| Ran from inside the package folder | Wrong working directory | Run from the root above the com folder |
| Error when running a JAR | Bad manifest Main-Class | Fix Main-Class to a class inside the JAR |
| Spring Boot app, java -jar fails | JAR not repackaged / many main classes | Use the Boot plugin; set mainClass (see section 6) |
The message appears in a few shapes, but they share one root. Here are the common ones:
Error: Could not find or load main class HelloWorld Error: Could not find or load main class com.app.Main Error: Could not find or load main class Main.java Caused by: java.lang.ClassNotFoundException: com.app.Main Caused by: java.lang.NoClassDefFoundError: Main (wrong name: com/app/Main)
That last line is a strong clue. A ClassNotFoundException means the JVM started fine but could not find your class on the classpath. So the problem is where you pointed it, not the code. A NoClassDefFoundError with “wrong name” is the other common variant. It means the JVM did find a .class file, but the package or capitalization inside it does not match the name you typed.
The JVM needs two things before your main method runs. It must locate the class file, and the class must match the name you typed. If either part is off, startup fails early.
So the fix is almost always about the run command, not the logic. Keep that in mind and the rest gets simple.
One file, one package. This single detail causes most real cases. File path: src/com/app/Main.java
package com.app;
public class Main {
public static void main(String[] args) {
System.out.println("It works!");
}
}Now watch it fail with the command most people try first:
javac src/com/app/Main.java java Main Error: Could not find or load main class Main Caused by: java.lang.ClassNotFoundException: Main
It fails because the real name is not Main. The package line makes it com.app.Main. The JVM takes you literally, hunts for a top-level Main, and finds nothing.
Start at the top. The first two solve the large majority of cases.
A class with a package line must run by its fully qualified, dotted name:
javac -d out src/com/app/Main.java java -cp out com.app.Main It works!
The -d out flag makes javac build the folder layout, so you get out/com/app/Main.class. Then -cp out points the JVM at the root, and you name the class with dots.
The classpath is where packages begin, not where the .class file sits. For out/com/app/Main.class, the root is out — the folder that contains com.
# Correct — root folder, dotted name java -cp out com.app.Main # Wrong — pointing at the leaf folder java -cp out/com/app Main
Separate multiple classpath entries with ; on Windows and : on Linux or Mac.
The java command wants a class name, not a file. So this fails:
java Main.class # wrong
There is one exception. Since Java 11 you can run a single file directly with java Main.java. That is source-file launch mode. It suits one small file, not a packaged project. For real code, compile first.
Java class names are case-sensitive. The class file keeps the exact name of the class, so java helloworld fails for a class named HelloWorld. Type it the way the source declares it.
Where you stand in the terminal matters when the classpath is relative. Move folders, and the same command can suddenly break. So list the files and confirm the class is really there:
# Linux / Mac find out -name '*.class' # Windows dir /s out\*.class
If out/com/app/Main.class does not show up, compilation went elsewhere. Recompile with -d out, or use an absolute classpath while you debug.
When a JAR throws this, the Main-Class in its manifest is usually wrong or missing. Look inside before you guess:
jar tf app.jar | grep Main unzip -p app.jar META-INF/MANIFEST.MF
Confirm the manifest reads Main-Class: com.app.Main and that com/app/Main.class is actually in the listing. If it is not, rebuild. With Maven, the Shade or Spring Boot plugin sets this for you.
Stale .class files confuse the JVM and cause odd cases that look impossible. A clean build clears them:
mvn clean package # or gradle clean build
In IntelliJ IDEA, use Build then Rebuild Project. In Eclipse, use Project then Clean. Then run once more.
Spring Boot adds its own twist. When you build a runnable JAR, Boot repackages it as a fat JAR, and the manifest does not point at your application class. So the usual guesses do not apply, and the error has a Spring-specific cause.
In a Boot fat JAR, the manifest Main-Class is a loader, not your app. The real class sits in a second entry, Start-Class. The loader reads it and starts Spring. You can see both yourself:
unzip -p app.jar META-INF/MANIFEST.MF Main-Class: org.springframework.boot.loader.launch.JarLauncher Start-Class: com.app.MyApplication
So two things commonly go wrong. Either the JAR was not repackaged by the Boot plugin, so there is no launcher. Or the build could not pick one main class because your project has several.
A plain jar has no launcher and no Start-Class. You usually see the cousin error no main manifest attribute. The fix is to let the Spring Boot plugin build the JAR. In Maven, add the plugin:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>Then run mvn clean package and launch with java -jar target/app.jar. In Gradle, the org.springframework.boot plugin and bootJar task do the same job.
If your project has more than one @SpringBootApplication class, the build cannot choose, and you get Unable to find a single main class. So name it clearly.
In Maven, set the main class on the plugin:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.app.MyApplication</mainClass>
</configuration>
</plugin>In Gradle, set it once in the springBoot block:
springBoot {
mainClass = 'com.app.MyApplication'
}When this happens only in your IDE, the run configuration usually names the wrong main class, or the module was not built. So open Run/Debug Configurations and confirm the Main class field points at your @SpringBootApplication class. Then do a clean rebuild of that module and run again.
One more quick tip. You can override the entry point at runtime without rebuilding, using the properties launcher:
java -cp app.jar -Dloader.main=com.app.MyApplication \
org.springframework.boot.loader.launch.PropertiesLauncherDo not assume. Confirm it in two quick steps.
java -cp out com.app.Main It works!
When your own output appears, the class loaded. That is the real proof.
javap prints the exact name the JVM will accept, so you can match your command to it:
javap -cp out com.app.Main
Compiled from "Main.java"
public class com.app.Main {
public com.app.Main();
public static void main(java.lang.String[]);
}If javap prints the class, your classpath and name are both correct, and java -cp out com.app.Main must work. If javap cannot find it either, go back to Fix 1 and Fix 2.
A: The class usually exists, but the JVM looks in the wrong place. If your class has a package line, run it with the full dotted name and point the classpath at the root folder, for example java -cp out com.app.Main.
A: For com.app.Main at out/com/app/Main.class, the classpath root is out — the folder that contains the com folder. Never point the classpath at the leaf folder that holds the .class file.
A: Yes. Since Java 11 you can run a source file directly with java Main.java, even when it has a package line. Since Java 22 the launcher can also compile other source files that it references. For larger projects, compile with javac or a build tool first.
A: The Main-Class in the JAR manifest is wrong or the class was never packaged. Run unzip -p app.jar META-INF/MANIFEST.MF and jar tf app.jar to confirm the Main-Class matches a class inside the JAR, then rebuild if needed.
A: Stale compiled files are the usual cause in an IDE. Use Build → Rebuild Project in IntelliJ (Project → Clean in Eclipse), then run again to clear old .class files that no longer match your package layout.
Let us wrap up. The could not find or load main class error is almost never about your code. It is about the run command. Most of the time, the class has a package line and you ran the short name, or the classpath points at the wrong folder.
So start with the two big fixes: run the full dotted name, and point -cp at the root folder that holds com. If that does not do it, work down the checklist in section 8. For a JAR, check the manifest. For Spring Boot, let the Boot plugin build the JAR and name your main class. Then confirm the fix with javap and a real run.