Question
A common problem for new Java developers is that a program fails to run with an error like this:
Error: Could not find or load main class MyClass
What does this error mean in Java, what are the usual causes, and how can you fix it correctly?
Short Answer
By the end of this page, you will understand what Java means by "Could not find or load main class", how Java locates classes when a program starts, and how to fix the most common causes such as wrong class names, incorrect package usage, bad classpaths, and trying to run a class that does not contain a valid main method.
Concept
When you run a Java program from the command line, the Java launcher tries to start a specific class.
For example:
java MyClass
Java then tries to:
- Find the compiled class file for
MyClass - Load that class into the JVM
- Locate a valid entry point:
public static void main(String[] args)
If Java cannot even locate or load the class you named, it reports:
Could not find or load main class ...
This usually means one of these things:
- The class name you ran is wrong
- The class was not compiled yet
- The class is in a package, but you used the wrong name
- The classpath does not include the directory or JAR containing the class
- You are running from the wrong working directory
- The compiled
.classfile is not where Java expects it to be
This matters because Java does not run source files the same way many scripting languages do. It runs compiled classes, and it uses the fully qualified class name plus the classpath to find them.
A key idea is this:
javaccompiles source code into.classfilesjavaruns a compiled class by name
Mental Model
Think of Java like a mail delivery system.
- The classpath is the list of buildings the mail carrier is allowed to search.
- The package name is the apartment number path.
- The class name is the person you want to deliver to.
If you say:
java MyClass
Java looks for someone named MyClass in the places listed in the classpath.
If the real class is actually:
package com.example;
then Java is not looking for just MyClass. It is looking for:
com.example.MyClass
and expects to find it in folders like:
com/example/MyClass.class
If the address does not match, Java cannot deliver the message, so it says it could not find or load the main class.
Syntax and Examples
Basic command
Compile:
javac MyClass.java
Run:
java MyClass
This works only if:
MyClass.javacompiled successfullyMyClass.classexistsMyClassis not in a package- Your current directory is on the classpath
Example without a package
public class MyClass {
public static void main(String[] args) {
System.out.println("Hello, Java");
}
}
Compile and run:
javac MyClass.java
java MyClass
Example with a package
File: src/com/example/MyClass.java
package com.example;
{
{
System.out.println();
}
}
Step by Step Execution
Consider this file:
com/example/App.java
package com.example;
public class App {
public static void main(String[] args) {
System.out.println("App started");
}
}
Compile:
javac com/example/App.java
This creates:
com/example/App.class
Now run:
java com.example.App
What Java does step by step
- Java receives the class name
com.example.App - It converts that name into a path like
com/example/App.class - It searches each location in the classpath
- If it finds the class file, it loads it
- It looks for this exact method:
public static void
Real World Use Cases
This error appears in many practical situations:
Running a small command-line app
A beginner writes a single Java file, compiles it, and then runs the wrong name or from the wrong directory.
Working with packages in a real project
Most real Java projects use packages such as:
package org.example.app;
You must run the class using the full package name:
java org.example.app.Main
Using build output folders
Many projects compile to folders like out, build/classes, or target/classes.
Example:
java -cp out com.example.Main
Running from a JAR
If the application is packaged into a JAR, Java must know where the class is.
You may run it with:
java -cp app.jar com.example.Main
or, if the JAR manifest defines the main class:
java -jar app.jar
IDE vs terminal differences
Real Codebase Usage
In real projects, developers usually avoid this error by relying on predictable structure and build tools.
Common patterns
Standard package structure
Classes are stored in directories that match package names:
src/main/java/com/example/app/Main.java
Build output directories
Compiled classes go into a known output folder such as:
target/classesin Maven projectsbuild/classes/java/mainin Gradle projects
Then developers run:
java -cp target/classes com.example.app.Main
Build tools define the entry point
Maven, Gradle, and IDEs often configure the main class so developers do not type it manually every time.
Validation and early checks
Developers often verify these first:
- Did compilation succeed?
- Is the package declaration correct?
- Does the folder structure match the package?
- Am I using the full class name?
- Is the classpath pointing to the root of compiled classes?
Error handling mindset
A useful habit is to separate the startup process into three questions:
- Was the code compiled?
- Can Java find the class?
Common Mistakes
1. Running the source file name instead of the class name
Broken:
java MyClass.java
Usually you should run:
java MyClass
after compiling with javac.
2. Including .class in the command
Broken:
java MyClass.class
Correct:
java MyClass
Java expects a class name, not a file name.
3. Ignoring the package name
If your file contains:
package com.example;
then this is wrong:
java MyClass
Correct:
java com.example.MyClass
4. Wrong folder structure for the package
Broken setup:
Comparisons
Similar Java startup problems
| Problem | What it means | Example fix |
|---|---|---|
Could not find or load main class | Java cannot locate or load the class you asked to run | Check class name, package, classpath, and working directory |
Main method not found in class | Java found the class, but there is no valid entry point | Add public static void main(String[] args) |
ClassNotFoundException | A class needed during execution could not be found | Fix the runtime classpath |
NoClassDefFoundError | A class existed at compile time but is missing or failed to load at runtime | Fix dependencies or classpath |
javac vs java
Cheat Sheet
Quick diagnosis checklist
If you see:
Could not find or load main class ...
check these in order:
- Did compilation succeed?
javac MyClass.java - Are you using the correct class name?
- Use
MyClass, notMyClass.javaorMyClass.class
- Use
- Is the class in a package?
- If yes, run the full name like
com.example.MyClass
- If yes, run the full name like
- Does the folder structure match the package?
com.example.MyClassshould be incom/example/MyClass.class
- Is the classpath correct?
java -cp . com.example.MyClass - Are you in the right directory?
- The classpath should point to the root above the package folders
Correct patterns
No package:
FAQ
Why does Java say it could not find or load the main class?
Because the JVM could not locate the class you asked it to run, or could not load it from the classpath.
Do I run java MyClass or java MyClass.java?
Usually java MyClass after compiling with javac MyClass.java.
Why does java MyClass fail when my file has package com.example;?
Because the real class name is com.example.MyClass. You must run the fully qualified class name.
What is the classpath in Java?
The classpath is the list of directories and JAR files Java searches for classes.
How do I fix the error if my class is in src/com/example?
Run from src with:
java com.example.MyClass
or from the parent directory with:
java -cp src com.example.MyClass
Is this the same as a missing main method?
Mini Project
Description
Create a tiny Java command-line program in a package and run it successfully from the terminal. This project helps you practice the exact concepts behind the error: package names, folder structure, compilation, and running with the correct class name.
Goal
Build and run a packaged Java program without triggering the "Could not find or load main class" error.
Requirements
- Create a Java class named
Appinside the packagecom.example. - Add a valid
public static void main(String[] args)method. - Print a short message when the program starts.
- Compile the program from the directory above
com. - Run the program using its fully qualified class name.
Keep learning
Related questions
Avoiding Java Code in JSP with JSP 2: EL and JSTL Explained
Learn how to avoid Java scriptlets in JSP 2 using Expression Language and JSTL, with examples, best practices, and common mistakes.
Choosing a @NotNull Annotation in Java: Validation vs Static Analysis
Learn how Java @NotNull annotations differ, when to use each one, and how to choose between validation, IDE hints, and static analysis tools.
Convert a Java Stack Trace to a String
Learn how to convert a Java exception stack trace to a string using StringWriter and PrintWriter, with examples and common mistakes.