How To Fix “Error: Could Not Find Or Load Main Class”?

If you’ve worked with Java, chances are you’ve encountered the frustrating “Error: Could not find or load main class.” This message pops up when the Java Virtual Machine (JVM) tries to run your program but can’t locate the crucial starting point – your main class.
It’s a common roadblock for developers at all levels, often bringing progress to a screeching halt. But don’t worry!
While it sounds intimidating, this error usually stems from a few simple missteps in how you’re telling Java to find or execute your code. This guide will help you understand why it happens and how to fix it.
How to Fix “Could Not Find or Load Main Class?”
Encountering the “Error: Could not find or load main class” can be a frustrating hurdle, but the good news is that the solutions are often straightforward.
By systematically checking a few common areas, you can typically get your Java program running smoothly. This section will walk you through the fixes for the most frequent causes of this error.
Solution for Incorrect Class Name
One of the simplest yet most common reasons for this error is a mismatch with the class name. Here’s how to fix it:
- Verify Spelling and Case: Java is case-sensitive. If your class is defined as MyMainClass in your .java file (and thus compiled to MyMainClass.class), then trying to run java mymainclass or java MyMainclass will fail. Carefully check that the name you type in the command line exactly matches the class name, including capitalization.
- Do Not Include the .class Extension: When you run your Java application using the java command, you should only provide the class name, not the filename with its extension. For example, if your compiled file is MyMainClass.class, you should run java MyMainClass, not java MyMainClass.class. The JVM automatically looks for the .class file.
- Ensure Class Name and File Name Match (for public classes): If your class is declared as public, the .java source file name must exactly match the public class name. For instance, a class public class MyProgram must be in a file named MyProgram.java. When compiled, this will produce MyProgram.class, and you will run it using java MyProgram.
Double-check the class name for typos and correct casing, and never include .class in your java command.
Solution for Classpath Problems

The classpath tells the Java Virtual Machine (JVM) where to look for user-defined classes and packages. If it’s not set up correctly, the JVM won’t find your main class.
- Navigate to the Correct Directory: By default, the JVM looks for classes in the current directory. If your MyMainClass.class file is located in C:\project\classes, you should either navigate your command prompt or terminal to this classes directory before running java MyMainClass, or specify the classpath.
- Specify the Classpath Explicitly: You can use the -cp or -classpath option with the java command to tell the JVM where to find your class files.
- If your .class file is in the current directory: java -cp . MyMainClass (though often the . is implied if no other classpath is set).
- If your .class file is in a subdirectory, for example, bin: java -cp bin MyMainClass. You would run this command from the directory containing bin.
- If your class files are in multiple locations or you need to include JAR files, separate them with a semicolon (;) on Windows or a colon (:) on Linux/macOS. For example: java -cp .;lib/somejar.jar MyMainClass (Windows) or java -cp .:lib/somejar.jar MyMainClass (Linux/macOS).
- Check the CLASSPATH Environment Variable: While less common for simple programs, an incorrectly configured global CLASSPATH environment variable can sometimes interfere. For most development, it’s preferable to set the classpath per execution using the -cp option. If you suspect this is an issue, you can check its value or temporarily unset it to see if it resolves the problem.
Also Read: 500+ Roblox Display Name Ideas for Gamers in 2025
Solution for Package Issues
When your Java classes are organized into packages, you must use their fully qualified names, and your directory structure must match your package structure.
- Use the Fully Qualified Class Name: If your MainClass is inside a package, say com.example.myapp, then its fully qualified name is com.example.myapp.MainClass. You must use this full name when executing the class: java com.example.myapp.MainClass.
- Run from the Correct Base Directory: You should run the java command from the directory that is the root of your package structure. For the example above (com.example.myapp.MainClass), if your class file is located at C:\project\classes\com\example\myapp\MainClass.class, you should navigate your terminal to C:\project\classes\ (the directory containing the com folder) and then run java com.example.myapp.MainClass. Running the command from within the myapp directory using just java MainClass will not work because the JVM won’t find the class within the expected package structure from that location.
- Ensure Directory Structure Matches Package Declaration: The physical directory structure on your file system must mirror the package declaration in your .java file. If you have package com.example.myapp; at the top of MainClass.java, then the MainClass.class file must be located inside a folder myapp, which is inside a folder example, which is inside a folder com.
When using packages, always use the fully qualified class name with the java command, and execute it from the base directory containing your top-level package folder. Ensure your folders match your package structure.
Solution for Missing/Uncompiled Class Files
Sometimes the error occurs simply because the .class file the JVM is looking for doesn’t exist where it expects it, or it was never created due to compilation issues.
- Check for Compilation Errors: Before trying to run your Java code, ensure it compiled successfully using the javac command. If there were any compilation errors, the .class file for your main class might not have been generated, or an older, incorrect version might exist. Recompile your .java file (e.g., javac MyMainClass.java or javac com/example/myapp/MainClass.java if using packages) and check for any error messages. Fix any reported errors and compile again.
- Verify .class File Existence and Location: After a successful compilation, verify that the .class file (e.g., MyMainClass.class) actually exists in the directory where the JVM is looking for it (as determined by your current directory and classpath settings).
- IDE Build Paths/Output Folders: If you are using an Integrated Development Environment (IDE) like IntelliJ IDEA, Eclipse, or VS Code, be aware that they often compile source files into a separate output directory (commonly named bin, out, target/classes, etc.). If you are trying to run your application from the command line outside of the IDE, you need to make sure you are either in that output directory or that your classpath points to it. For example, if your IDE compiles to a bin folder, you might need to run java -cp bin com.example.myapp.MainClass.
Always confirm your code compiled without errors and that the .class file for your main class exists in the expected location before attempting to run it.
Also Read: How To Stop The Logitech G403 From Disconnecting? Fix Guide
Key Takeaways to Avoid This Error

Preventing this error often comes down to careful habits and understanding the basics of how Java executes code:
- Double-Check Names: Always be meticulous with class names, ensuring they match exactly in your code, file names (for public classes), and when you type the java command (case sensitivity matters!).
- Understand Your Classpath: Know where your .class files are and how to tell the JVM to find them, either by being in the correct directory or by using the -cp option.
- Mind Your Packages: If using packages, always use fully qualified class names for execution and ensure your directory structure mirrors your package declarations. Run java from the base directory of your package structure.
- Compile First, Then Run: Make it a habit to check for compilation errors and confirm .class file generation before attempting to run your application.
- Be Aware of IDEs vs. Command Line: If you switch between an IDE and command-line execution, understand where your IDE places compiled files and adjust your commands or classpath accordingly.
- Start Simple: When learning or starting a new project, begin with a simple class without packages to ensure your basic compilation and execution steps are correct. Then, introduce packages and other complexities incrementally.
Conclusion
The “Error: Could not find or load main class” in Java, while initially perplexing, usually boils down to straightforward issues like incorrect class names, classpath misconfigurations, package declaration mismatches, or missing compiled files.
By systematically checking these common culprits and understanding how Java locates and runs your code, you can quickly diagnose and resolve this frequent error, keeping your development process moving forward smoothly.