Home › Java

Error: Could not find or load main class — how to fix

Error: Could not find or load main class Main
Caused by: java.lang.ClassNotFoundException: Main

Quick fix: Run from the classpath root with the fully qualified name, without .class: javac -d out src/com/example/Main.java && java -cp out com.example.Main. For a single file on Java 11+: java Main.java.

What it means

java X searches the classpath (by default the current directory) for X.class, respecting packages as folders. If it can't find it, you get this error.

Cause 1: you included .class or .java

java Main.class      # wrong
java Main            # right

(On Java 11+, java Main.java is valid: it compiles and runs a single source file.)

Cause 2: the class is in a package

package com.example;
public class Main { ... }

The compiled file must live at com/example/Main.class and you must use the full name from the root folder:

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

Running java Main inside com/example/ fails because the JVM expects com.example.Main.

Cause 3: wrong classpath

If you set -cp or the CLASSPATH environment variable, the current directory is no longer included automatically:

java -cp lib/gson.jar Main          # can't find Main
java -cp lib/gson.jar:. Main        # Linux/macOS
java -cp "lib/gson.jar;." Main      # Windows

Check for a stray global CLASSPATH: echo $CLASSPATH.

Cause 4: running a jar

java -jar app.jar

needs Main-Class: com.example.Main in META-INF/MANIFEST.MF. With Maven, configure maven-jar-plugin (or Spring Boot's plugin); with Gradle, set application { mainClass = 'com.example.Main' } and use ./gradlew run or build a fat jar.

Cause 5: IDE out of sync

IntelliJ: Build → Rebuild Project, check the Run Configuration's main class and module, or File → Invalidate Caches. Eclipse: Project → Clean.

Cause 6: special characters in the path

Paths with non-ASCII characters or % / ! can break class loading on some systems. Move the project to a simple path.

Last updated 2026-10-27.