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

  • Last Updated: October 24, 2026
  • By: javahandson
  • Series
img

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

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.

1. Causes at a Glance

What you see / didReal causeQuick fix
Class has a package line, you ran the short nameWrong class nameRun the full dotted name: java com.app.Main
java Main.class or java Main.javaExtension in commandDrop it: java Main
java main (lowercase)Wrong capitalizationMatch the class exactly: java Main
-cp points at the folder holding the .classWrong classpath rootPoint at the package root, not the leaf folder
Ran from inside the package folderWrong working directoryRun from the root above the com folder
Error when running a JARBad manifest Main-ClassFix Main-Class to a class inside the JAR
Spring Boot app, java -jar failsJAR not repackaged / many main classesUse the Boot plugin; set mainClass (see section 6)

2. The Exact Error Message

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.

3. Why This Happens

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.

4. Smallest Example That Breaks

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.

5. The Fixes, In Priority Order

Start at the top. The first two solve the large majority of cases.

5.1 Fix 1 — Use the full class name with its package

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.

5.2 Fix 2 — Point the classpath at the root, not the leaf

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.

5.3 Fix 3 — Drop the .java and .class extension

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.

5.4 Fix 4 — Match the capitalization exactly

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.

5.5 Fix 5 — Check your working directory

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.

5.6 Fix 6 — Fix a broken JAR

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.

5.7 Fix 7 — Clean and rebuild

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.

6. The Same Error in a Spring or Spring Boot App

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.

6.1 Why it happens in Spring Boot

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.

6.2 Fix A — Make sure the Boot plugin repackaged the JAR

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.

6.3 Fix B — Tell the build which main class to use

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'
}

6.4 Fix C — Running from the IDE

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.PropertiesLauncher

7. How to Verify the Fix Worked

Do not assume. Confirm it in two quick steps.

7.1 Step 1 — Run it and watch for your output

java -cp out com.app.Main
It works!

When your own output appears, the class loaded. That is the real proof.

7.2 Step 2 — Ask javap for the name the JVM expects

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.

8. One-Minute Checklist

  • Package line present? Run the full dotted name.
  • Classpath pointing at the root, not the leaf folder?
  • Extension dropped from the run command?
  • Capitalization matching the source exactly?
  • Standing in the right working directory?
  • For a JAR, Main-Class matches a class inside it?
  • Still stuck? Clean build, then try once more.

9. FAQ’s on could not find or load main class

Q: Why do I get “could not find or load main class” when the class clearly exists?

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.

Q: What is the difference between the classpath root and the folder with the .class file?

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.

Q: Can I run a Java file without compiling it first?

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.

Q: I get this error running a JAR. How do I fix it?

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.

Q: IntelliJ shows “could not find or load main class” — what do I do?

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.

10. Conclusion

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.

11. Further Reading

Leave a Comment