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- The name and folder structure must match the package declaration
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;
public class MyClass {
public static void main(String[] args) {
System.out.println("Hello, packaged Java");
}
}
Compile from src:
javac com/example/MyClass.java
Run from src using the fully qualified name:
java com.example.MyClass
Notice:
- You do not run
java MyClass - You do not include
.class - You do not use slashes in the class name
Running with an explicit classpath
java -cp . MyClass
or for a packaged class:
java -cp . com.example.MyClass
Here, . means “search the current directory for class files and package folders.”
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 main(String[] args)
- If found, the program starts
What happens if you run the wrong name
If you run:
java App
Java looks for:
App.class
in the classpath root.
But your class is actually inside com/example/App.class, so Java cannot find the requested class and reports the error.
What happens if you run from the wrong directory
Suppose your folder layout is:
project/
src/
com/
example/
App.class
If you are inside project/src, this can work:
java com.example.App
If you are inside project, it may fail unless the classpath points to src:
java -cp src com.example.App
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
An IDE often hides classpath and package details. A program may run in IntelliJ or Eclipse but fail in the terminal because the terminal command is missing the correct classpath or package-qualified class name.
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?
- Does the class have a valid
mainmethod?
That simple checklist solves most startup issues quickly.
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:
MyClass.java
with source code:
package com.example;
Correct setup:
com/example/MyClass.java
The package and directory structure should match.
5. Running from the wrong directory
If your classes are under src, this may fail:
java com.example.MyClass
from the project root.
Correct:
java -cp src com.example.MyClass
or run from inside src.
6. The class was never compiled
If there is no .class file, Java cannot load it.
Compile first:
javac MyClass.java
7. Confusing this error with a missing main method
If Java finds the class but there is no valid main method, the error is different, such as:
Error: Main method not found in class MyClass
That means the class was found, but the entry point is missing or incorrect.
8. Misspelling the class name
Java names are case-sensitive.
Broken:
java myclass
Correct:
java MyClass
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
| Command | Purpose | Input | Output |
|---|---|---|---|
javac | Compiles Java source code | .java files | .class files |
java | Runs compiled Java bytecode | Class name or JAR | Program execution |
Simple class name vs fully qualified class name
| Form | Example | When to use |
|---|---|---|
| Simple class name | MyClass | Only when the class is in the default package |
| Fully qualified class name | com.example.MyClass | When the class belongs to a package |
Default package vs named package
| Approach | Example | Notes |
|---|---|---|
| Default package | public class MyClass | Fine for tiny experiments, not for real projects |
| Named package | package com.example; | Standard for real Java codebases |
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:
javac MyClass.java
java MyClass
With package:
javac com/example/MyClass.java
java com.example.MyClass
Explicit classpath:
java -cp . com.example.MyClass
From another output folder:
java -cp out com.example.MyClass
Important rules
javatakes a class name, not a file name- Do not include
.javaor.class - Package name must match directory structure
- Java is case-sensitive
- The classpath must point to the root containing the package folders
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?
No. If the class is found but the entry point is missing, Java gives a different error about the main method.
Why does it work in my IDE but not in the terminal?
Your IDE automatically sets the classpath, output directory, and main class. In the terminal, you must provide them correctly yourself.
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
Add External JAR Files to an IntelliJ IDEA Java Project
Learn how to add external JAR dependencies to an IntelliJ IDEA Java project using module libraries, and when to use Maven or Gradle instead.
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.
Call a Method After a Delay in Android Java
Learn how to run Java code after a delay in Android using Handler.postDelayed, manage the main thread, and cancel callbacks safely.