When working with Java, the final execution of code happens via pre-compiled classes. Once you write a class in Java, the compiler converts your high-level source code into a low-level scheme called bytecode. For each class that you write, an instance of a pre-compiled bytecode is generated. This bytecode contains the low-level code that can be run on a Java Virtual Machine.
However, sometimes you might have come across a situation in which running the java terminal command returns something like: could not find or load main class
. This indicates that there is some issue with your class’s bytecode. But, how do you fix it? Let’s find out.
What Does ‘could not find or load main class’ Mean?
The error could not find or load main class
indicates that there are some issues with your code design. It usually occurs when you run a command like the following on your terminal to run a Java program:
java Test
Here, “Test” is the name of the class that contains your code that has to be executed. If you face a could not find or load main class
error, it means that the bytecode for the Test class could not be found.
How to Fix ‘could not find or load main class’ Error?
There are several ways in which you can fix the error. Here are some of the top ways to identify and fix could not find or load main class
:
Check For Typos
The first probable cause of this error might be a typing mistake. Since the syntax of the program call consists of only two keywords — “java” and class name — the only place where you can make a mistake is the class name. If you were to make a typo in the keyword, you would receive an obvious “command not found” error on the command line.
You can try double-checking the class name for typing mistakes. More often than not, such errors originate from silly spelling and typing mistakes.
Let’s say you wrote a class called “Test” in the file “Test.java”. This is how you would normally compile it via the command line:
javac Test.java
And then you would execute it by running the following command:
java Test
This should run your program just fine. However, if you misspelled “Test” in the above command, you might face a Could not find or load main class
error.
Incorrect Classpath in Application
There are multiple ways in which an application’s classpath can cause this error. The classpath (written as CLASSPATH) is an environment variable that points to the location where user classes are stored in the system. The directory that the classpath points to contains multiple pre-written classes that you can directly import in your code.
Let’s say you wrote the following import command:
Import org.company.Foo;
Running this will make the JVM look for the “Foo” class at the location <classpath>/org/company/Foo.java. As you can see, if the classpath points to the wrong address, JVM will never be able to find the class that you are looking for, and hence throw the could not find or load main class
error.
Sometimes, the classpath might not contain all system classes that your program depends upon. This means that maybe you are trying to include a class from a package that you haven’t downloaded or set-up yet. In that case too, you will face this error.
The Class Has Been Declared In The Wrong Package
If the package written on top of your source code file does not point to the package/module that the class has been declared in. Most IDEs can help you figure this out very easily by showing an error in the linting process. However, if it slips the eye, it will cause the above error.
The best solution to this issue is to always check that the package name of a class points to the correct package. If you use an advanced IDE like IntelliJ or VSCode, make sure to enable linting for this issue.
For instance, take the example of the following class stored at project/src/com/foo/bar:
package com.foo.baz; class Bar { // Some code here }
As you can see, the package points to com/foo/baz while the class is actually stored in com/foo/bar. To fix this, you need to update the package statement to point to the right package:
package com.foo.bar; class Bar { // Some code here }
Conclusion
In this article, we took a look at what Could not find or load main class
is in Java, and why it occurs. Then, we concluded our discussion by identifying some of the top ways to fix a Could not find or load main class
error.
If you are looking to write programs in Java, this error may pop up from time to time.
While the error message might seem quite terrifying, it is not too difficult to solve. All you need to do is to check if the class that you are looking for is in the right location or not.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.