Question

What "Could Not Find or Load Main Class" Means in Java

javaclassprogram-entry-point

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:

  1. Find the compiled class file for MyClass
  2. Load that class into the JVM
  3. 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 .class file 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:

  • javac compiles source code into .class files
  • java runs 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.

Take Quiz

Syntax and Examples

Basic command

Compile:

javac MyClass.java

Run:

java MyClass

This works only if:

  • MyClass.java compiled successfully
  • MyClass.class exists
  • MyClass is 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

  1. Java receives the class name com.example.App
  2. It converts that name into a path like com/example/App.class
  3. It searches each location in the classpath
  4. If it finds the class file, it loads it
  5. It looks for this exact method:
public static void main(String[] args)
  1. 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.

Take Quiz

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/classes in Maven projects
  • build/classes/java/main in 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:

  1. Was the code compiled?
  2. Can Java find the class?
  3. Does the class have a valid main method?

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

ProblemWhat it meansExample fix
Could not find or load main classJava cannot locate or load the class you asked to runCheck class name, package, classpath, and working directory
Main method not found in classJava found the class, but there is no valid entry pointAdd public static void main(String[] args)
ClassNotFoundExceptionA class needed during execution could not be foundFix the runtime classpath
NoClassDefFoundErrorA class existed at compile time but is missing or failed to load at runtimeFix dependencies or classpath

javac vs java

CommandPurposeInputOutput
javacCompiles Java source code.java files.class files
javaRuns compiled Java bytecodeClass name or JARProgram execution

Simple class name vs fully qualified class name

FormExampleWhen to use
Simple class nameMyClassOnly when the class is in the default package
Fully qualified class namecom.example.MyClassWhen the class belongs to a package

Default package vs named package

ApproachExampleNotes
Default packagepublic class MyClassFine for tiny experiments, not for real projects
Named packagepackage com.example;Standard for real Java codebases
Take Quiz

Cheat Sheet

Quick diagnosis checklist

If you see:

Could not find or load main class ...

check these in order:

  1. Did compilation succeed?
    javac MyClass.java
    
  2. Are you using the correct class name?
    • Use MyClass, not MyClass.java or MyClass.class
  3. Is the class in a package?
    • If yes, run the full name like com.example.MyClass
  4. Does the folder structure match the package?
    • com.example.MyClass should be in com/example/MyClass.class
  5. Is the classpath correct?
    java -cp . com.example.MyClass
    
  6. 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

  • java takes a class name, not a file name
  • Do not include .java or .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.

Take Quiz

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

  1. Create a Java class named App inside the package com.example.
  2. Add a valid public static void main(String[] args) method.
  3. Print a short message when the program starts.
  4. Compile the program from the directory above com.
  5. Run the program using its fully qualified class name.
Take Quiz

Keep learning

Related questions

View more