Monday, September 26, 2022

Java - Could not find or load main class error Fix

In this post we'll see when you can get "Could not find or load main class error" in Java and how to resolve it. From the error itself it is evident when you are trying to run the program JVM is not able to find a class with main method and start the execution.

Could not find or load main class error and resolution

Two scenarios where you may encounter this error are-

  1. Not using the correct class name while executing the program.
  2. Not using the fully qualified class name and from the correct path.

Let's try to see both scenarios with an example.

Here is a simple Java program written in a class named "MyJavaClass" and saved in a file name "MyJavaClass.java"

public class MyJavaClass{
	public static void main(String[] args){
		System.out.println("This is my Java program");
	}
}

You can compile this Java program using the javac command and passing the full file name.

javac MyJavaClass.java

Once the file is compiled a .class file with the exact same name as the class name in your Java program is generated in the same directory where the java file resides.

In order to run the program, you will have to use the java command and passing the name of the .class file. That's when you can make a mistake where you can get "Could not find or load main class error"

1. Not using the correct class name

Since the generated class file has the exact same name as the class name which means class file in our example is generated as MyJavaClass.class. If you need to run your program, you will have to use the following command.

C:\>java MyJavaClass
This is my Java program

If you deviate in any way from this name you will get the error.

C:\>java Myjavaclass
Error: Could not find or load main class Myjavaclass
Caused by: java.lang.NoClassDefFoundError: MyJavaClass (wrong name: Myjavaclass)

Here name is given as Myjavaclass instead of MyJavaClass thus the error.

2. Even the extension .class is not required.

As you can see while executing the program you have to give the name of the class with out the extension .class. Giving the full name while trying to execute also results in an error.

C:\>java MyJavaClass.class
Error: Could not find or load main class MyJavaClass.class
Caused by: java.lang.ClassNotFoundException: MyJavaClass.class

Class in a package

In the above example we have not used any package but having a class within a package may also cause the "Could not find or load main class error" if fully qualified class name is not used and from the correct path.

Let's try to include a package in our example program.

package com.netjstech;

public class MyJavaClass{
	public static void main(String[] args){
		System.out.println("This is my Java program");
	}

}

You can use the following command to compile it so that the correct folder structure as per the package name given is created.

C:\>javac -d . MyJavaClass.java

With this command you will see that the .class file is created with in the folder com\netjstech. You may think that by going to that location you should be able to run the program.

C:\com\netjstech>java MyJavaClass
Error: Could not find or load main class MyJavaClass
Caused by: java.lang.NoClassDefFoundError: com/netjstech/MyJavaClass (wrong name: MyJavaClass)

As you can see now even after providing the correct class name (java MyJavaClass) still there is an error.

Class is in the package now, so you need to use the fully qualified class name (package + class name) to locate the class. Fully qualified class name in our case is- com.netjstech.MyJavaClass

C:\com\netjstech >java com.netjstech.MyJavaClass
Error: Could not find or load main class com.netjstech.MyJavaClass
Caused by: java.lang.ClassNotFoundException: com.netjstech.MyJavaClass

As you can see even that results in an error because you are already at the location (com\netjstech) and then you are using the fully qualified class name too. When you use the fully qualified class name Java itself tries to use that full name to find the correct location.

In our case when we give the fully qualified name (java com.netjstech.MyJavaClass), Java tries to look for the class MyJavaClass with in com\netjstech from the current path which itself is C:\com\netjstech.

So, you need to go two levels up and then use the fully qualified name to run this program successfully.

C:\> java com.netjstech.MyJavaClass
This is my Java program

That's all for this topic Java - Could not find or load main class error Fix. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Basics Tutorial Page


Related Topics

  1. static Reference to The Non-static Method or Field Error
  2. Why Class Name And File Name Should be Same in Java
  3. Why main Method static in Java
  4. java.lang.ClassCastException - Resolving ClassCastException in Java
  5. Core Java Basics Interview Questions And Answers

You may also like-

  1. Difference Between StackOverflowError and OutOfMemoryError in Java
  2. final Keyword in Java With Examples
  3. Deadlock in Java Multi-Threading
  4. HashMap Vs LinkedHashMap Vs TreeMap in Java
  5. Interface Static Methods in Java
  6. Compare Dates in Java
  7. Angular @Input and @Output Example
  8. Spring JdbcTemplate Insert, Update And Delete Example

No comments:

Post a Comment