Windows

Fix: Could not find or load main class

The error ‘Could not find or load main class’ occurs when using a java command in the command prompt to launch a Java program by specifying the class name in the terminal. The reason why this happens is mostly due to the user’s programming mistake while declaring the class.

Could not find or load main class in Java command line
Could not find or load main class- Java command line

Like mentioned before, this error is mostly not related to the system and the user makes a mistake in several scenarios as shown below. Before moving forward, we assume that you have a basic knowledge of Java programming language and how it works.

What causes the error ‘Could not find or load main class’ in Java?

The message ‘Could not find or load main class’ means that the first step of the Java engine fetching the class for execution has failed. The java command was not able to locate the class at the correct directory.

In some cases, you have to add the correct file path and point the Java terminal to the correct location. Since you are executing the command from the command line terminal, the computer does not know where the class is to be found or where it is located. In targeted IDE’s, this isn’t the problem as the IDE keeps a pointer pointing towards the current working directory.

What is ‘java <class-name>’ syntax?

Before we start troubleshooting why the terminal is returning us an error when trying to execute, first we need to look at the syntax of the command. If you are not using the correct syntax, you will face this error inevitably.

The normal syntax of the command is something like this:

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

Here <option> is a command line option, <class-name> is a fully qualified Java class name, and <argument> is a command line argument which is passed to your application when the whole package is compiled.

An example of a valid command is:

java -Xmx100m com.acme.example.ListAppuals kevin arrows bart

The above command will make the java command execute the following operations:

  • It will search for a compiled version of the class ‘com.acme.example.ListAppuals’.
  • After searching, it will load the class.
  • Next, when the class is loaded, the class will be searched for a ‘main’ method with a valid signature, modifiers, and return type. A sample main class would be something like:
public static void main(String[])
  • The method will be called with arguments ‘kevin’, ‘arrows’, and ‘bart’ as string[].

How to Fix ‘Could not find or load main class’

Solution 1: Checking class name argument

The most common mistake users make is that they provide a wrong class name as an argument (or a correct class name is a wrong form). Since we are declaring parameters on the command line, it is highly likely that you will pass the class name argument in a wrong form. Here we will list all the possible scenarios where you can make a mistake.

  • Writing a simple class name. If you declare the class in a package such as ‘com.acme.example’, you must use the full classname including the package in the Java command.
java com.acme.example.ListAppuals

instead of

java ListAppuals
  • You should declare a classname instead of declaring a filename or pathname. Java does not fetch the class if you declare a pathname/filename for it. Incorrect entries include the following:
java ListAppuals.class

java com/acme/example/ListAppuals.class
  • Casing should be taken into consideration. Java commands are case sensitive and if you make a mistake of even one letter, you will not be able to load the main class. An example of incorrect mistakes are:
java com.acme.example.listappuals
  • You should not declare a source filename. Like mentioned before, you only need to declare the class in the correct full classname format. Example of a mistake is:
java ListAppuals.java
  • This error will also occur if you make a typing mistake or forget to write the class name entirely.

If you have made any sloppy mistakes in declaring the classname, make sure you fix it and then try launching the program.

Solution 2: Checking classpath

If you have declared the classname correctly but are still shown the error, chances are that java command could not find the specified classname at the path. The classpath is a path where Java runtime searches for resource and class files. You can easily set the classpath using two different commands as shown below:

C:> sdkTool -classpath classpath1;classpath2...

C:> set CLASSPATH=classpath1;classpath2...

To get more insight regarding the classpath, you should check out the following documents.

Java command documentation

Setting classpath

Solution 3: Checking directory

When you declare a directory as a classpath, it will always correspond to the root of namespace. For example if “/usr/local/acme/classes” is on the classpath, then Java will search for a class “com.acme.example.Appuals”. It will look for a class with the following pathname:

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

So in essence, if you put the following address in the classpath, Java will not be able to find the class:

/usr/local/acme/classes/com/acme/example

You should also check your subdirectory and see if it matches the FQN. If your classes FQN is “com.acme.example.Appuals”, then Java will search for an “Appuals.class” in the directory “com/acme/example”.

To give you an example, let’s suppose the following scenario:

  • The class you want to run is: com.acme.example.Appuals
  • The full file path is: /usr/local/acme/classes/com/acme/example/Appuals.class
  • The current working directory is: /usr/local/acme/classes/com/acme/example/

Then the following scenarios would hold:

# wrong, FQN is needed

java Appuals

# wrong, there is no `com/acme/example` folder in the current working directory

java com.acme.example.Appuals

# wrong, similar to above scenario

java -classpath . com.acme.example.Appuals

# OK ; a relative classpath is set

java -classpath ../../.. com.acme.example.Appuals

# OK; an absolute classpath is set

java -classpath /usr/local/acme/classes com.acme.example.Appuals

Note: The classpath needs to also include all the other classes (non-system) which your applications needs.

Solution 4: Checking class package

If all the above solutions hold true in your case, you need to make sure that your source code is placed in the correct folder. Also, you have correctly declared the package. If you run your code with an IDE, it will probably inform you regarding the issue. However, in our case, since we are running it in command prompt, the mistake will go unnoticed and you will get the class error under discussion.

Expert Tip

Fix: Could not find or load main class

If the issue is with your Computer or a Laptop you should try using Reimage Plus which can scan the repositories and replace corrupt and missing files. This works in most cases, where the issue is originated due to a system corruption. You can download Reimage by clicking the Download button below.

Download Now

I'm not interested

Close