CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > How to > (Solved) Error: Could Not Find or Load Main Class in Java

Java Tutorial Menu

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor

(Solved) Error: Could Not Find or Load Main Class in Java

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Published: August 29, 2020 · 3 min read · 0 Comments
Share: X in 🔗
Error: Could Not Find or Load Main Class

Introduction

If you’re a Java developer or a student just starting to learn Java, you are likely to encounter the Error: Could not find or load main class at some point. This error can be frustrating, but fortunately, it’s usually easy to fix. In this post, we’ll go over the most common causes of this error and how to solve them.

Solution #1: The Main Class is Not Specified Correctly

One of the most common causes of the “Error: Could not find or load main class” is that the main class is not specified correctly. When you run a Java program, you need to specify the main class that contains the main() method. If you specify the wrong class or if you mistype the class name, you’ll see this error.

To fix this problem, make sure that you are specifying the correct main class and that you have typed the class name correctly. The correct main class looks like below:

public class MainClass {

	public static void main(String[] args) {

               // Do something
	}
}

Solution #2: The Main Class is Not in the Classpath

Another common cause of this error is that the main class is not in the classpath. The classpath is a list of directories and JAR files that Java searches when it needs to find a class. If the main class is not in the classpath, Java won’t be able to find it and you’ll see the “Error: Could not find or load main class” message.

To fix this problem, make sure that the main class is in the classpath. You can specify the classpath when you run the Java program using the -classpath or -cp option. For example:

java -classpath /path/to/main/class MainClass

Solution #3: The Main Class is in a Package

If the main class is in a package, you’ll need to specify the full package name when you run the Java program. For example, if the main class is in the com.example package, you’ll need to specify the full package name like this:

java com.example.MainClass

Make sure to use the correct package name and to use the proper package hierarchy. If you get the package name wrong or if you forget to specify the package, you’ll see the “Error: Could not find or load main class” message.

Solution #5

If you’re still having trouble, try compiling and running your program from the command line using the following command.

Let’s say we have a class HelloWorld.java and it compiled successfully. Then we can execute the following command and it should work.

java -cp ./classes;. HelloWorld

Conclusion

The “Error: Could not find or load main class” is a common error in Java, but it’s usually easy to fix. By following the tips in this post, you should be able to solve this error and get your Java program up and running in no time.

Related Posts:

  • Top 10 Common Java Errors
  • Building a Simple JDBC Project: Student Management System
  • Control Statements in Java
  • MySQL Commands for Developers
  • Identifiers in Java
  • How to Solve “systemctl command not found” Error: A…
Tags:java
Was this article helpful?
← Previous ArticleJava DateTimeFormatter Example: A Complete Guide to Date Formatting
Next Article →How to change Git remote URL

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
  • Complete Guide to JaCoCo: How to Measure Java Code Coverage Accurately
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap