What does “Could not find or load main class” mean?

A common problem that new Java developers experience is that their programs fail to run with the error message: Could not find or load main class …

What does this mean, what causes it, and how should you fix it?

“Could not find or load main class” What does this mean?

I have never programmed with Java before. I tried making a HelloWorld program, following instructions from a website. This is what my program looks like: import java.applet.*; import java.awt.*; publi



Could not find or load main class error

For some reason I am getting this error Error: Could not find or load main class SumOfPolynovmials but this is a class that I have never created. Why am I getting this error? It should be running Su

Playing sound files with Java – Error: Could not find or load main class [Classname] [duplicate]

This question already has an answer here: What does “Could not find or load main class” mean? 6 answers I’m trying to (do something as ‘simple’ as) getting java to play a sound file. I’ve got t

Java error: Could not find or load main class memoryarray.MemoryArray

I have a java program where I got the following error: Error: Could not find or load main class memoryarray.MemoryArray Java Result: 1 What does the error mean?

whats the reason for the “could not find or load main class ”error? [duplicate]

This question already has an answer here: What does “Could not find or load main class” mean? 7 answers more explanation:while writing the regular HELLO WORD code I always face this error-not

Don’t understand why I’m getting the Error: Could not find or load main class from console [duplicate]

This question already has an answer here: What does “Could not find or load main class” mean? 7 answers I have looked over several other answers on this site trying to understand why this might

Could not find or load main class

I have Windows 7, installed jdk1.7.0 and its supporting jre7. My problem is compilation part works perfectly, but while running the Java program I get this error saying Could not find or load main cl

What does “Error: Could not find or load main class org.apache.hadoop.util.RunJar”?

I am trying to run an example as it is pointed in Hadoop in Action book page 15. this is the command that needs to be run : bin/hadoop jar hadoop-*-examples.jar but I get this error Error: Could n

Java Error: could not find or load main class Vehicle

I am trying to load Vehicle class after I compiled it successfully but this error occures: Error: could not find or load main class Vehicle I think that there may be a mistake in my code, which is the

Error: Could not find or load main class

I have problem running the following code, it says Error: Could not find or load main class but i clearly have a main in the code… package abc; import java.io.IOException; import javax.swing.JOptio

Answers

The java <class-name> command syntax

First of all, you need to understand the correct way to launch a program using the java (or javaw) command.

The normal syntax1 is this:

    java [ <option> ... ] <class-name> [<argument> ...]

where <option> is a command line option (starting with a “-” character), <class-name> is a fully qualified Java class name, and <argument> is an arbitrary command line argument that gets passed to your application.

1 – There is a second syntax for “executable” JAR files which I will describe at the bottom.

For example:

    java -Xmx100m com.acme.example.ListUser fred

What this is going to do is the following:

  1. Search for the compiled version of the com.acme.example.ListUser class.
  2. Load the class.
  3. Check that the class has a main method with signature static void main(String[]).
  4. Call that method passing it the command line arguments as a String[].

Reasons why Java cannot find the class

When you get the message “Could not find or load main class …”, that means that the first step has failed. The java command was not able to find the class. And indeed, the “…” in the message will be the fully qualified class name that java is looking for.

So why might it be unable to find the class? Basically, there are two main causes:

The first likely cause is that you may have provided the wrong class name. (Or … the right class name, but in the wrong form.) Considering the example above, here a variety of wrong ways to specify the class name:

  • Example #1 – a simple class name:

    ListUser
    
  • Example #2 – a filename or pathname rather than a class name:

    ListUser.class
    com/acme/example/ListUser.class
    
  • Example #3 – a class name with the casing incorrect:

    com.acme.example.listuser
    
  • Example #4 – a typo

    com.acme.example.mistuser
    

The second likely cause is that the class name is correct, but that the java command cannot find the class. To understand this, you need to understand the concept of the “classpath”. This is explained well by the Oracle documentation:

So … if you have specified the class name correctly, the next thing to check is that you have specified the classpath correctly:

  1. Read the three documents linked above. (Yes … READ them.)
  2. Look at command line and / or the CLASSPATH environment variable that is in effect when you run the java command. Check that the directory names and JAR file names are correct.
  3. If there are relative pathnames in the classpath, check that they resolve correctly … from the current directory that is in effect when you run the java command.
  4. Check that the class (mentioned in the error message) can be located on the effective classpath.

Additional Notes:

  1. When you put a directory on the classpath, it notionally corresponds to the root of the qualified name space. Classes are located in the directory structure beneath that root, by mapping the fully qualified name to a pathname. So for example, if “/usr/local/acme/classes” is on the class path, then when the JVM looks for a class called com.acme.example.Foon, it will look for a “.class” file with this pathname:

        /usr/local/acme/classes/com/acme/example/Foon.class
    

    If you had put “/usr/local/acme/classes/com/acme/example” on the classpath, then the JVM wouldn’t be able to find the class.

  2. The classpath needs to include all of the other (non-system) classes that your application depends on. (The system classes are located automatically, and you rarely need to concern yourself with this.)

The java -jar <jar file> syntax

The alternative syntax used for “executable” JAR files is as follows:

    java [ <option> ... ] -jar <jar-file-name> [<argument> ...]

e.g.

    java -Xmx100m -jar /usr/local/acme-example/listuser.jar fred

In this case the name of the entry-point class (i.e. com.acme.example.ListUser) and the classpath are specified in the MANIFEST of the JAR file.

IDEs

A typical Java IDE has support for running Java applications in the IDE JVM itself or in a child JVM. These are generally immune from this particular exception, because the IDE uses its own mechanisms to construct the runtime classpath, identify the main class and create the java command line.

However it is still possible for this exception to occur, if you do things behind the back of the IDE to break things. For example, if you have previously set up an Application Launcher for your Java app in Eclipse, and you then moved the JAR file containing the “main” class to a different place in the file system without telling Eclipse, Eclipse would unwittingly launch the JVM with an incorrect classpath.

In short, if you get this problem in an IDE, check for things like stale IDE state and broken project references or launcher configurations.

Other References

Sometimes what might be causing the issue has nothing to do with the main class. I had to find this out the hard way, it was a referenced library that I moved and it gave me the:

Could not find or load main class xxx Linux

I just delete that reference and added again and it worked fine again.

First set the path using this command;

set path="paste the set path address"

Then you need to load the program. Type “cd (folder name)” in the stored drive and compile it. For Example, if my program stored on the D drive, type “D:” press enter and type ” cd (folder name)”.

In my case, error appeared because I had supplied the source file name instead of the class name.

We need to supply the class name containing the main method to the interpreter.

If your source code name is HelloWorld.java, your compiled code will be HelloWorld.class

You will get that error if you call it using :

java HelloWorld.class

Instead, use this :

java HelloWorld

Hope it helps.

What helped me was specifying the classpath in command line, for example:

  1. Create new folder C:/temp

  2. Create file Temp.java in C:/temp, with following class in it:

    public class Temp {
        public static void main(String args[]) {
            System.out.println(args[0]);
        }
    }
    
  3. Open command line in folder C:/temp, write following command to compile Temp class:

    javac Temp.java
    
  4. Run compiled java class adding -classpath option to let JRE know where to find the class:

    java -classpath C:/temp Temp Hello!
    

If your main method is in the class under a package. you should run it over the hierarchical directory.
Assume there is a source code(Main.java):

package com.test;

public class Main {

public static void main(String[] args) {
    System.out.println("salam 2nya/n");
  }
}

For run this code, you should place Main.Class in the package like directory. ./come/test/Main.Java and in root directory use java come.test.Main

If your classes are in packages then you have to cd to main directory and run using the full name of the class (packageName.MainClassName)

Example:

my classes are in here

D:/project/com/cse/

full name of my main class is

com.cse.Main

so I cd back to the main dir

D:/project

then issue the java command

java com.cse.Mian

In the instance you have:

Could not find or load main class ?classpath

It’s because you are using “-classpath” but the dash is not the same dash used by java in the command prompt. I had this issue copying and pasting from notepad to cmd.

When the same code works in one PC but shows the error in another, the best solution I have ever found is compiling like the following:

> javac HelloWorld.java
> java -cp . HelloWorld

I spent a decent amount of time trying to solve this problem. I thought that I was somehow setting my classpath incorrectly but the problem was that I typed:

java -cp C:/java/MyClasses C:/java/MyClasses/utilities/myapp/Cool
instead of:
java -cp C:/java/MyClasses utilities/myapp/Cool
I thought the meaning of fully qualified meant to include the full path name instead of the full package name.

According to the error message (“Could not find or load main class”), there are two categories of problems:

  1. Main class could not be found
  2. Main class could not be loaded (this case is not fully discussed in the accepted answer)

Main class could not be found when there is typo or error syntax in the fully qualified class name or it does not exist in the provided classpath.

Main class could not be loaded when the class cannot be initiated, typically the main class extends another class and that class does not exist in the provided classpath.

For example:

public class YourMain extends org.apache.camel.spring.Main

If camel-spring is not included, same error will be reported.

In Java, when you sometimes run the JVM from the command line using the java executable and are trying to start a program from a class file with public static void main (PSVM), you might run into the below error even though the classpath parameter to the JVM is accurate and the class file is present on the classpath:

Error: main class not found or loaded

This happens if the class file with PSVM could not be loaded. One possible reason for that is that the class may be implementing an interface or extending another class that is not on the classpath. Normally if a class is not on the classpath, the error thrown indicates as such. But, if the class in use is extended or implemented, java is unable to load the class itself.

Reference: https://www.computingnotes.net/java/error-main-class-not-found-or-loaded/

On Windows put .; at CLASSPATH value in beginning.
The . (dot) means “look in current directory”.
This is permanent soultion.

Also you can set it “one time” with set CLASSPATH=%CLASSPATH%;.
This will last as long as your cmd window is open.

I got this error after doing mvn eclipse:eclipse This messed up my .classpath file a little bit.

Had to change the lines in .classpath from

<classpathentry kind="src" path="src/main/java" including="**/*.java"/>
<classpathentry kind="src" path="src/main/resources" excluding="**/*.java"/>

to

<classpathentry kind="src" path="src/main/java" output="target/classes" />
<classpathentry kind="src" path="src/main/resources" excluding="**"  output="target/classes" />

This is a specific case, but since I came to this page looking for a solution and didn’t find it, I’ll add it here.

Windows (tested with 7) doesn’t accept special characters (like á) in class and package names. Linux does, though.

I found this out when I built a .jar in NetBeans and tried to run it in command line. It ran in NetBeans but not in command line.

What fixed the problem in my case was:

Right click on the project/class you want to run, then Run As->Run Configurations. Then you should either fix your existing configuration or add new in the following way:

open the Classpath tab, click on the Advanced… button then add bin folder of your project.

I was unable to solve this problem with the solutions stated here (although the answer stated has, no doubt, cleared my concepts). I faced this problem two times and each time I have tried different solutions(in Eclipse IDE).

  • Firstly, I have come across with multiple main methods in different classes of my project. So, I had deleted main method from subsequent classes.
  • Secondly, I tried following solution:
    1. Right click on my main project directory.
    2. Head to source then clean up and stick with the default settings and on Finish. After some background tasks you will be directed to your main project directory.
    3. After that I close my project and reopen it and boom I finally solved my problem.

You really need to do this from the src folder. There you type the following command line:

[name of the package].[Class Name] [arguments]

Let’s say your class is called: CommandLine.class

and the code looks like this:

package com.tutorialspoint.java;

    /**
     * Created by mda21185 on 15-6-2016.
     */

    public class CommandLine {
        public static void main(String args[]){
            for(int i=0; i<args.length; i++){
                System.out.println("args[" + i + "]: " + args[i]);
            }
        }
    }

Then you should cd to the src folder and the command you need to run would look like this:

java com.tutorialspoint.java.CommandLine this is a command line 200 -100

and the output in command line would be:

args[0]: this
args[1]: is
args[2]: a
args[3]: command
args[4]: line
args[5]: 200
args[6]: -100

sometimes its better to remove the added jar files and add again with proper build helps. For me it has been a regular issue and I follow the same approach.

  1. put all the referred jar files in a folder jarAddOns and copy it in a safe place
  2. now from eclipse (or from your IDE) remove the jar files .
  3. move the whole project folder from workspace to a safe location
  4. restart eclipse (your IDE)
  5. now import your project directory from the safe location.
  6. add the jar files into your project from the jarAddOns folder (previously saved in safe location)
  7. project buildpath and add jars and apply
  8. now run the project it should not show the error.

To solve this on Eclipse, I replaced JRE in my build path with JDK, and then Right click on the project Run As->Run Configurations-> Classpath tab-> Advancedthen add bin folder of my project, and it works!!

Try -Xdiag.

Steve C’s answer covers the possible cases nicely, but sometimes to determine whether the class could not be found or loaded might not be that easy. Use java -Xdiag (since jdk 7). This prints out nice stacktrace which provides a hint to what the message Could not find or load main class message means.

For instance, it can point you to other classes used by the main class that could not be found and prevented the main class to be loaded.

If your class is in a package, make sure to set the package declaration to say so. I spent 15 minutes debugging mine only to realize I had put the class in its folder but not put the package declaration in the file.

I had such error in this case:

java -cp lib.jar com.mypackage.Main

works with ; for Windows and : for Unix

java -cp lib.jar; com.mypackage.Main

I got same error while starting webapp server in STS. Fixed this issue by looking at VM arguments of server.

All answers here are directed towards Windows users it seems. For MAC the classpath seperator is : not ; . As an error setting the classpath using ; is not thrown then this can be a difficult to discover if coming from Windows to Mac.

Here is corresponding MAC command :

java -classpath ".:./lib/*" com.test.MyClass

where in this example the package is com.test and a lib folder is also to be included on classpath.

I had this problem in jGrasp. I deleted the corresponding .class file and then re-compiled and it worked just fine.

IDK what caused it.

From the Option menu, select ProjectClean and then select your project and click OK. Your project will run perfectly after this cleaning.

For database connection i was getting this one: just added following in classpath export CLASSPATH=/db2jcc4.jar:./

here i appended ./ in last so that my class also get identified while loading now just run: java ConnectionExample worked perfectly fine.

Seems like when I had this problem, it was unique.

Once I removed the package declaration at the top of the file, it worked perfectly.

Aside from doing that, there didn’t seem to be any way to run a simple HelloWorld.java on my machine, regardless of the folder the compilation happened in, the CLASSPATH or PATH, parameters or folder called from.

Use this command

java -cp . [PACKAGE.]CLASSNAME

example if your classname is Hello.class created from Hello.java then use below command

java -cp . Hello

If your file Hello.java is inside package com.demo then use below command

java -cp . com.demo.Hello

with jdk 8 many time it happens that class file is present in same folder but java command expects classpath and for this reason we add -cp . to take current folder as reference for classpath.

What does “Could not find or load main class” mean?

Class file location: C:/test/com/company

File Name: Main.class

Fully qualified class name: com.company.Main

Command line command:

java  -classpath "C:/test" com.company.Main

Note here that class path does NOT include /com/company

By default, Java uses ., the geek letter for “current working directory” (you now know one letter in the geek alphabet right?) as the default CLASSPATH. What this means is that when you type a command at the prompt e.g. java MyClass, the command is interpreted as if you had type java -cp . MyClass. Did you see that dot between -cp and MyClass? (cp is short for the longer classpath option)

This is sufficient for most cases and things seems to work just fine until at some time you try to add a directory to your CLASSPATH. In most cases when programmers need to do this, they just run a command like set CLASSPATH=path/to/some/dir. This command creates a new environment variable called CLASSPATH having the value path/to/some/dir or replaces its value with path/to/some/dir if CLASSPATH was already set before.

When this is done, you now have a CLASSPATH environment variable and Java no longer uses it’s default classpath (.) but the one you’ve set. So the next day you open your editor, write some java program, cd to the directory where you saved it, compile it, and try to run it with the command java MyClass, and you are greeted with a nice output: Could not find or load main class … (If your commands were working well before and you are now getting this output, then this might be the case for you).

What happens is that when you run the command java MyClass, Java searches for the class file named MyClass in the directory or directories that you have set in your CLASSPATH and not your current working directory so it doesn’t find your class file there and hence complains.

What you need to do is add . to your class path again which can be done with the command set CLASSPATH=%CLASSPATH%;. (notice the dot after the semicolon). In plain english this command says “Pick what was initially the value of CLASSPATH (%CLASSPATH%), add . to it (;.) and assign the result back to CLASSPATH”.

And viola, you are once again able to use your command java MyClass as usual.

Thanks.

When running the java with the -cp option as advertised in Windows PowerShell you may get an error that looks something like:

The term `ClassName` is not recognized as the name of a cmdlet, function, script ...

In order to for PowerShell to accept the command, the arguments of the -cp option must be contained in quotes as in:

java -cp 'someDependency.jar;.' ClassName

Forming the command this way should allow Java process the classpath arguments correctly.

This might help you if your case is specifically like mine: as a beginner I also ran into this problem when I tried to run a java program.

I compiled it like this: javac HelloWorld.java

and tried to run also with the same extension

java Helloworld.java

When I removed the .java and rewrote the command like this java HelloWorld, The Program ran perfectly. 🙂

On windows, it is case sensitive. Therefore Java –jar Jenkins.war

didnt run, instead we had to remove casing to java –jar Jenkins.war

if you use maven to build the jar please making sure to specify the main class in the pom.xml

     <build>
        <plugins>
          <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
              <archive>
                <manifest>
                  <mainClass>class name us.com.test.abc.MyMainClass</mainClass>
                </manifest>
              </archive>
            </configuration>
          </plugin>
        </plugins>
    </build>