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 ’ 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: Here
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:
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.
instead of
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:
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:
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:
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: 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: So in essence, if you put the following address in the classpath, Java will not be able to find the class: 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.AppualsThe full file path is: /usr/local/acme/classes/com/acme/example/Appuals.classThe current working directory is: /usr/local/acme/classes/com/acme/example/
Then the following scenarios would hold: 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.
Motorola Razr 2 to Reportedly have Bigger Main And Front Displays: Main Display…Fix: Could not load Internet Accounts preference paneFix: Could not Load ‘Citizengame.DLL’ in FiveMFix: Could not Load File or Assembly ‘RSy3_AudioAppStreamsWrapper.dll