“Could not find or load main class”
If you get an error message that reads Could not find or load main class while trying to launch a Java program through a command in the command prompt then the reason behind the issue is likely a mistake on your side and most probably has to do with a wrongly declared class. In such a case, the command is unable to locate the directory where the class is located. Sometimes, what’s required is for the user to provide the correct path to the needed file. Below, we will cover the most common instances when this error is likely to show up and we will also help you deal with it.
How to Fix “Could not find or load main class” in Java
Method 1: Fixing the class name argument
Usually, the mistake that users make which leads to the Java could not find or load main class is the class name that they give as an argument is wrong or, if the class name is correct, the form is wrong. Here are the possible ways you can get this wrong:
If you write a simple class name – for instance, if the declared class is in a package such as com.acme.example, you should use the full classname and also include the package when typing the command. Here is an example:
Type java com.acme.example.howtoremove rather than just java howtoremove.
Also, remember that a classname must be declared and not a pathname (or filename). Here are several examples of how you can do that:
- java ListHowtoremove.class
- java com/acme/example/ListHowtoremove.class
Casing is also important in this case – even a slight error such as a single incorrect letter could cause the Java could not find or load main class problem. For example, the following entry is incorrect and would thus not allow the main class to load:
- java com.acme.example.listhowtoremove
As said above, using a filename will not lead to the desired result. Here is an example of a incorrect entry:
- java ListHowtoremove.java
Method 2: Fixing the classpath
If the previous method didn’t work (i.e. the classname has been correctly declared) and if the error still seems to persists, then the likely reason for it to keep on showing is that the entered command is unable to find the classname path which, in turn, results in the Java could not find or load main class error. Luckily, you can correctly set the classpath with either of the following commands so make sure to use them:
C:> sdkTool -classpath classpath1;classpath2…
C:> set CLASSPATH=classpath1;classpath2…
Method 3: Directory check
A directory would always correspond to namespace’s root when it is declared as a classpath and so if, for instance, on the classpath is /usr/local/acme/classes, Java’s search will be for com.acme.example.Howtoremove. In this case, the looked for class will have /usr/local/acme/classes/com/acme/example/Appuals.class as a pathname.
Basically, if you use this address /usr/local/acme/classes/com/acme/example as classpath, the class will not be found by Java.
Remember that you will also need to check the subdirectory as it needs to match the FQN. For example, if the FQN of your classes is com.acme.example.Howtoremove, Java would try to find an Howtoremove.class in the com/acme/example directory.
Leave a Comment