How To Fix Error Could Not Find or Load Main Class

Error – Could not load or find main class Programming in Java programming language requires a main() method in order to run/execute them. This is because it is the place where...

Error – Could not load or find main class

Programming in Java programming language requires a main() method in order to run/execute them. This is because it is the place where program execution starts. Sometimes, Java programs might display the error “Couldn’t find or load main classes”This error is caused by an incorrect attempt to execute main() within the class using the java command.

Reasons for Occurring Error:

  • File Extension
  • Package wrong
  • Invalid Classpath
  • Wrong class name

What is the reason for the “JVM couldn’t find or load main class” error?

The ‘couldn’t find or load main classes’ error is a runtime error that indicates that the JVM ( Java Virtual Machine ) cannot locate the main code in your code. Is it possible to not find it?

This is one of Java’s most unexpected and unpredicted errors. It occurs because JVM tends to use a default path. The “main class not found problem” haunts both professionals and amateurs alike. It isn’t as serious as it looks, but it is easy to fix. This article will show you how to fix the annoying Java compilation error.

How do I fix ‘Couldn’t find or load mainclass’?

There are two ways to correct the error:

Use Exact Classname instead .class file

The exact classname must be used instead of the.class files to execute the program. Use the Java classname, for example, to compile the following syntax.


It will result in an error. Instead, we must type.

style=’color:#EAEAEA;background:black’>java Program1

Find and Fix Syntax Errors

  • Syntax errors can be reduced by checking the syntax and case of classnames before declaring them. If we write the syntax,

style=’color:#EAEAEA;background:black’>javac program1

If you try to compile it then there will be an error. Type the syntax

style=’color:#EAEAEA;background:black’>java Program1style=’font-size:11.0pt;color:black’>

Define class-path

  • If an error occurs, check the classpath. The classpath must be set to the current directory. To set the classpath in Windows, use the syntax below

style=’color:#EAEAEA;background:black’>CLASSPATH = %CLASSPATH%;.

The current directory is indicated by the dot at its end.

Check Spelling and Case

  • When you type the command, make sure to check the spelling and case of the classname. If we type incorrectly, it will cause an error.

Java is case sensitive and you need to be aware of casing.


style=’color:#EAEAEA;background:black’>java Progam1style=’font-size:11.0pt;color:black’>

File Extension

We must give the Java program file name and extension to compile it (. Java).

#FAFAFA’>$ class=language-bash>style=’box-sizing: border-box’>javac HelloWorld.javastyle=’font-size:12.5pt;color:black’>

However, to run a. file we must provide the class name and not the filename. The .class extension is not required.

#FAFAFA’>$ class=language-bash>style=’box-sizing: border-box’>java HelloWorld.classstyle=’color:black;background:#FAFAFA’>style=’background:white’>Error: Could not find or load main class HelloWorld.classstyle=’font-size:12.5pt;color:black’>

Let’s use the correct class name to run our program again:

#FAFAFA’>$ class=language-bash>style=’box-sizing: border-box’>java HelloWorldstyle=’color:black;background:#FAFAFA’> style=’background:white’>Hello world..!! !style=’font-size:12.5pt;color:black’>

Java Package Names

Java keeps similar classes together in what is called a package.

Let’s make HelloWorld classes part of the com.baeldung bundle:

background:#FAFAFA’>packagestyle=’color:black;background:#FAFAFA’> com.baeldung;style=’background:white’> style=’background:white’>background:#FAFAFA’>publicstyle=’color:black;background:#FAFAFA’> class=hljs-keyword>classstyle=’color:black;background:#FAFAFA’> class=hljs-title>HelloWorldstyle=’color:black;background:#FAFAFA’> {style=’background:white’> style=’box-sizing: border-box’>style=’color:#63B175;background:#FAFAFA’>publicstyle=’color:black;background:#FAFAFA’> class=hljs-keyword>staticstyle=’color:black;background:#FAFAFA’> class=hljs-keyword>voidstyle=’color:black;background:#FAFAFA’> class=hljs-title>mainclass=hljs-params>style=’box-sizing: border-box’>(String[] args)style=’color:black;background:#FAFAFA’> {style=’background:white’> System.out.println(style=’box-sizing: border-box’>style=’color:#4E9359;background:#FAFAFA’>”Hello world..!!! style=’color:black;background:#FAFAFA’>);style=’background:white’> }style=’background:white’>}style=’font-size:12.5pt;color:black’>

Let’s now compile and run the HelloWorld program as before.

style=’color:black;background:#FAFAFA’>$ java HelloWorldstyle=’background:white’>Error: Could not find or load main style=’box-sizing: border-box’>style=’color:#63B175;background:#FAFAFA’>classstyle=’color:black;background:#FAFAFA’> class=hljs-title>HelloWorldstyle=’font-size:12.5pt;color:black’>

We get the error “Couldn’t find or load mainclass HelloWorld.”

Let’s see if we can figure out what went wrong.

To run a Java class that is in a package, we must provide its fully qualified name. So in our case, HelloWorld‘s fully qualified name is com.baeldung.HelloWorld.

This folder structure was actually created when we created .com.baeldung packages:


Let’s first try to run our program using the .com/baeldung directory.

#FAFAFA’>$ class=language-bash>style=’box-sizing: border-box’>java com.baeldung.HelloWorldstyle=’color:black;background:#FAFAFA’>style=’background:white’>Error: Could not find or load main class com.baeldung.HelloWorldstyle=’font-size:12.5pt;color:black’>

We are still unable to operate our program.

Here, when we specified the fully qualified class name com.baeldung.HelloWorld, Java tried to find the HelloWorld.class file in com/baeldung, under the directory from where we were running the program.

We were already in com/baeldung and Java could not find or run the HelloWorld programme.

Let’s go back to the parent folder, and then run it.

#FAFAFA’>$ class=language-bash>style=’box-sizing: border-box’>java com.baeldung.HelloWorldstyle=’color:black;background:#FAFAFA’>style=’background:white’>Hello world..!! !style=’font-size:12.5pt;color:black’>

We are able to again say “Hello” and welcome the world.

Invalid Classpath

Let’s first learn what the classpath means before we move on. It is the list of classes that are available to our current JVM.

The classpath variable tells the JVM where to locate .class files.

We can use to provide the classpath while running a program:

style=’color:black;background:#FAFAFA’>java -classpath /my_programs/compiled_classes HelloWorldstyle=’font-size:12.5pt;color:black’>

Here, Java will look for the HelloWorld.class file in /my_programs/compiled_classes folder, a folder whose name we just made up. The default classpath variable is “.”, which refers to the current directory.

We have changed the directory where our program is stored in the previous section. What if we want it to be run from another folder? The classpath variable is useful here.

We can say that our classpath contains two directories — one for each package. This will allow us to run our program from the com/baeldung directory.

#FAFAFA’>$ class=language-bash>style=’box-sizing: border-box’>java -claspath ../../ com.baeldung.HelloWorldstyle=’color:black;background:#FAFAFA’>style=’background:white’>Hello world..!! !style=’font-size:12.5pt;color:black’>

“..” is the parent directory. “../../” is the top of our package hierarchy.


The “Error Cannot Find a Class or Load Main Class” error can be seen. It is clear that basic syntax errors are responsible for this problem. These errors can be prevented by paying attention to the classpath settings and the correct casing of Java classes.

No Comment

Leave a Reply




error: Content is protected !!