Core Java

Could Not Find or Load Main Class in Java

In this tutorial, we will explain the error: could not find or load main class, the most common runtime exception in Java programming. The error usually occurs if the java command is unable to find or load the class that contains the main() method.

1. Introduction

Before digging deep into the solution let us understand a few reasons for this error to occur i.e.

could not find or load main class java
  • The error is generated when the Java Virtual Machine is unable to load the main class or the package name i.e. the JVM (Java Virtual Machine) does not find a .class file with the given name
  • A class declared in the wrong package
  • Dependencies missing from the CLASSPATH
  • Incorrect CLASSPATH specification
  • A human error while specifying the class name

But before going any further I am hoping that you are aware of the basics of the java programming language.

2. Solutions to the Java Error: could not find or load main class

To avoid this error or exception we will look at two ways where we will consider a class inside a java package and another way i.e., the class is not defined in a java package. For brevity, we considering the class is placed in the correct folder and in case the folder is incorrect the same exception would be thrown at runtime.

2.1 Without package

Consider a class containing a main() is defined as below.

Demo.java

public class Demo {
  public static void main(String[] args) {
    System.out.print("Hello world from javacodegeek.");
  }
}

Now let us head over to the command prompt that generates and the error on the command prompt console. We will generate the .class file using the javac command but while calling it we will do a human error that will help to understand the error. Do remember that the same error could also occur even if you specify the correct class name but add a .class suffix to it.

could not find or load main class java - generating the error
Fig. 1: Generating the error if the class is not defined inside the package

To fix this error we will simply need to class the with the correct name i.e., java Demo.java.

could not find or load main class java - fixing the error
Fig. 2: Fixing the error if the class is not defined inside the package

2.2 With package

In this part let us create a new class containing the main() under a package named – com.jcg.assignment. Consider a class inside the given package containing a main() is defined as below.

Example.java

package com.jcg.assignment;

public class Example2 {
  public static void main(String[] args) {
    System.out.print("Hello world from example2.");
  }
}

On compiling the program the .class file will be generated inside the given package. Now to generate a human error we will call the class without the package name as shown in Fix. 3.

could not find or load main class java - if the class is defined inside the package
Fig. 3: Generating the error if the class is defined inside the package

To fix this error we will simply need to specify the class name including the package name i.e., java com.jcg.assignment.Example2.java.

Fig. 4: Fixing the error if the class is defined inside the package

That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!

3. Summary

In this tutorial, we showed:

  • What exactly is the error: could not find or load main class
  • Steps to solve it.

4. Download the Eclipse Project

This was an example of solving the error: could not find or load main class in Java.

Download
You can download the full source code of this example here: Could Not Find Or Load Main Class in Java

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button