How to Fix 'Could not find or load main class'
In this tutorial, you'll learn about How to Fix 'Could not find or load main class. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
The Problem
You compile a Java program successfully but get this at runtime:
Error: Could not find or load main class HelloWorld
Caused by: java.lang.NoClassDefFoundError: HelloWorld (wrong name: com/example/HelloWorld)
Or simply:
Error: Could not find or load main class HelloWorld
The .class file exists, but Java cannot find it or refuses to load it. This error is almost always a classpath or naming problem.
Quick Fix
1. Match the class name to the filename
In Java, the public class name must match the filename exactly:
// File: HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Compile and run:
javac HelloWorld.java
java HelloWorld
Notice: no .class extension in the java command. Java looks for HelloWorld.class, not HelloWorld.java.
2. Include the full package name in the command
If your class has a package declaration, you must use the fully qualified name:
// File: HelloWorld.java
package com.example;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Compile and run from the root of the package tree:
# Compile (from src/ directory)
javac src/com/example/HelloWorld.java -d out/
# Run (from out/ directory -- the root of the package hierarchy)
java -cp . com.example.HelloWorld
The fully qualified name is com.example.HelloWorld, not HelloWorld.
3. Set the classpath correctly
If your class depends on other JAR files, include them with -cp:
java -cp .:lib/* com.example.HelloWorld
On Windows, use ; instead of ::
java -cp .;lib/* com.example.HelloWorld
Common classpath mistakes:
- Forgetting the current directory (
.). - Using the wrong path separator.
- Specifying the JAR directory but using
*incorrectly.
4. Check the manifest file in JARs
If you package your application as a JAR:
jar cfm app.jar MANIFEST.MF -C out/ .
The MANIFEST.MF must contain:
Main-Class: com.example.HelloWorld
Note the trailing newline -- the manifest requires a blank line at the end. Check with:
jar tf app.jar
unzip -p app.jar META-INF/MANIFEST.MF
5. Fix Maven build issues
If using Maven, ensure your pom.xml has the correct main class:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.example.HelloWorld</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
Run:
mvn clean package
java -jar target/app.jar
6. Fix Gradle build issues
For Gradle, configure the application plugin:
plugins {
id 'application'
}
application {
mainClass = 'com.example.HelloWorld'
}
Then:
./gradlew run
Or build and run manually:
./gradlew jar
java -jar build/libs/app.jar
Prevention
- Always compile from the source root, not from inside the package directory.
- Use a build tool (Maven or Gradle) -- it handles classpath and packaging automatically.
- Keep source files in a directory structure that mirrors the package hierarchy.
- Run
javap com.example.HelloWorldto verify the class has amainmethod.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro