Eclipse
Java
Error Resolution
Programming
Troubleshooting

Eclipse Error Could not find or load main class

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

This Java error means the JVM did not find the compiled class it was told to run, or it found a class name that does not match the real package and output layout. In Eclipse, that usually points to one of a few concrete problems: a broken run configuration, a package mismatch, or a build output issue. The fix is to verify the class name and classpath step by step instead of treating it as a mysterious Eclipse failure.

Make Sure the Main Class and Package Actually Match

The class you run must match its package declaration and its filesystem location. A simple Java example looks like this.

java
1package com.example;
2
3public class MyApp {
4    public static void main(String[] args) {
5        System.out.println("Hello");
6    }
7}

If this file declares package com.example;, Eclipse expects it in the matching source folder path, and the run configuration should reference com.example.MyApp, not only MyApp.

A mismatch between the declared package and the actual project structure is one of the most common causes of the error.

Check the Run Configuration in Eclipse

Eclipse stores a run configuration that names the project and the main class. If that entry points to an old class, a renamed package, or the wrong project, the JVM starts with the wrong target.

The quickest fix is often:

  1. open Run Configurations,
  2. select the Java Application entry,
  3. verify the project name,
  4. verify the full main class name,
  5. or delete the run configuration and create it again.

This matters especially after refactoring packages or copying classes between projects.

Rebuild the Project Output

Sometimes the source file is correct but the compiled .class output is stale or missing. Eclipse may have auto-build disabled, or the output folder may be misconfigured.

A clean rebuild often resolves that classpath state.

bash
# Equivalent command-line idea
javac -d bin src/com/example/MyApp.java
java -cp bin com.example.MyApp

The command-line example is useful because it isolates the problem. If the class runs from the terminal, your Java code is probably fine and the problem is in Eclipse configuration. If it fails there too, the package or build output is likely wrong.

Verify the Source Folder and Output Folder

In Eclipse, the Java Build Path defines which folders contain source code and where compiled classes are written. If the file sits outside the configured source folder, Eclipse may show the code but not compile it into the expected output tree.

That produces exactly the kind of “could not find or load main class” behavior that feels confusing because the file visibly exists in the project explorer.

The important question is not “does the source file exist”. It is “did Eclipse compile it into the runtime classpath using the correct package path”.

Watch for Typos and Case Sensitivity

Java class names are case-sensitive. So are package names on many file systems used in development and build pipelines. A small mismatch such as myapp versus MyApp or com.Example versus com.example can break class loading even when the names look visually similar.

This is especially easy to miss when switching between platforms or importing older projects.

External Libraries Usually Are Not the Cause

This specific error is about loading the main class itself, not about missing third-party dependencies later in startup. Do not jump to library management first. Confirm the main class, package, source folder, and run config before looking at anything else.

That order of operations saves time because the root cause is usually local and simple.

Common Pitfalls

  • Running MyApp when the real class name should be com.example.MyApp.
  • Refactoring packages without updating or recreating the Eclipse run configuration.
  • Storing the file outside the configured Java source folder.
  • Assuming the source file exists means the class was compiled into the runtime classpath.
  • Missing a case mismatch in the package or class name.

Summary

  • “Could not find or load main class” means the JVM could not locate the compiled entry class it was asked to run.
  • In Eclipse, the common causes are package mismatch, stale run configuration, or build output problems.
  • Verify the full class name, not only the short class name.
  • Rebuild the project and check the source and output folder mapping.
  • Isolate with a command-line javac and java test if Eclipse behavior is still unclear.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.