Could not find or load main class

In this post, we will discuss Could not find or load main class. Did you get this error before? If you have good amount of experience in Java programming or as a novice java developer, you might have encountered this error: Could not find or load main class.

Before we understand about this error, let’s first understand how to run java command syntax.

java <classpath> [ <option> … ] class-name [<argument> …]

Here

  • java is command to execute java program.
  • classpath represents -classpath or -cp option to put any folder or jar on classpath.
  • option represents command line option with “-” such as -DXmx1024M to specify maximum heap space as 1024 MB.
  • class-name represents name of the class which you want to execute
  • Agruments represents command line argument you want to pass to program.It will be passed to public static void main(args[])

When you specify class name to your program, you need to provide full classified class name.
For example:

If you want to execute above class and Let’s assume class files are generated in C:\workspace\javaProject\target\classes folder. You need to specify command as below.

C:\workspace\javaProject\target\classes>java -Xmx1024M org.java2blog.FirstProgramInJava

Please note that -Xmx1024M is optional here.

Here are the steps which take place when you run the java command.

  • Search compiled version of the class
  • Load the class
  • Check the class has correct main method signature
  • Call the methods with arguments.

Let’s understand what can be reasons for Could not find or load main class.

Essentially, there can be two categories for these problems.

  • Could not find main class
  • Could not load main class

Could not find main class

The most likely reason is that you might have provided wrong file name. Let’s understand the reasons for this error.


1. Classname without fully qualified classname

As stated before, you need to provide fully classified class name to run the java command.

For example:

You tried to run the class as below.

C:\workspace\javaProject\target\classes>java FirstProgramInJava

rather than it should be called as

C:\workspace\javaProject\target\classes>java org.java2blog.FirstProgramInJava

2. You have put .class as suffix.

This can be mostly likely reason as well.

For example:

C:\workspace\javaProject\target\classes>java org.java2blog.FirstProgramInJava.class

Remove .class extension to solve the issue


3. You have put .java as suffix.

You have put source filename directly to execute the command.
For example:

C:\workspace\javaProject\target\classes>java org.java2blog.FirstProgramInJava.java

Remove .java extension to solve the issue


4. Incorrect case

It may be due to an incorrect case. Carefully observe the classname.

C:\workspace\javaProject\target\classes>java org.java2blog.firstprograminjava

5. A typo

Check the spelling of the class again! It might be very unlikely but there can be typo in class name.

C:\workspace\javaProject\target\classes>java org.java2blog.FirstProgramJava

Could not load main class


1. Not able to find class due to subdirectory path.

Let’s say you have below FirstProgramInJava class.

You compiled the class. Now you want to run org.java2blog.FirstProgramInJava
.class file is generated in C:\workspace\javaProject\target\classes\org\java2blog\FirstProgramInJava.class
Current working directory is C:\workspace\javaProject\target\classes\org\java2blog

then Let’s understand what will happen when you run the following commands.

# Won’t run as the Fully qualified name is needed over here
C:\workspace\javaProject\target\classes\org\java2blog>java FirstProgramInJava
# Won’t run because there is no directory as org\java2blog in current working directory
C:\workspace\javaProject\target\classes\org\java2blog>java org.java2blog.FirstProgramInJava

It will look for org/java2blog/FirstProgramInJava.class in current directory but it won’t be able to find as we are already in org\java2blog directory.

You need to use -classpath or -cp to run FirstProgramInJava class from current working directory.

#This should work fine as we have put relative classpath here
C:\workspace\javaProject\target\classes\org\java2blog>java -cp ../.. org.java2blog.FirstProgramInJava
#This should work fine as we have put absolute classpath here
C:\workspace\javaProject\target\classes\org\java2blog>java -cp C:\workspace\javaProject\target\classes org.java2blog.FirstProgramInJava

2. You have put the wrong directory on the classpath

Whenever you put any directory on classpath then classes are searched on the basis of that directory.

For example:
You have put C:\workspace\javaProject\target\classes in classpath and you run below command

C:\workspace>java -cp C:\workspace\javaProject\target\classes org.java2blog.FirstProgramInJava

then it will look for .class file in C:\workspace\javaProject\target\classes\org\java2blog\FirstProgramInJava.class and will work fine but let’s say you run below command.

C:\workspace>java -cp C:\workspace\javaProject\target\classes\org\java2blog org.java2blog.FirstProgramInJava

It will not be able to find the class as it will look for .class in C:\workspace\javaProject\target\classes\org\java2blog\org\java2blog\FirstProgramInJava.class


3. You have missed dependencies from classpath

The class path should include all the libraries on which your application depends on.
If JVM need to load the class then

  • JVM should be able to find classes and interfaces in superclass hierarchy.
  • It should also be able to find all the classes and interfaces that are referred by variable, method or any other access expression.

That’s all about Could not find or load main class. If you know any other reason for this issue, feel free to comment.

Was this post helpful?

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *