Here’s How to Fix the “Could Not Find or Load Main Class” Error in Java

April 28, 2022
CouldNotFindorLoadMainClassErrorinJava Xblog



Introduction

Java developers often face the ‘could not find or load main class’ error out of the blue during compilation. If you keep getting this error without any specific reason, you are not alone. Whether you’re just starting out as a programmer or have some experience under the belt, we’ve all seen this error at least once. And we know it has nothing to do with our code.

Why does the “JVM could not find or load the main class” error occur?

As the name suggests, the ‘could not find or load main class’ error means that the JVM (Java Virtual Machine) could not locate the main class in your code and throws this runtime error. The question is can we not find it?

It is one of the most unpredicted and spontaneous errors in Java, which occurs due to the tendency of JVM to stick with a default classpath, the “main class not found issue” is something that haunts amateurs and professionals alike. As serious as it seems, it is not that difficult to fix. We will be exploring in this article how you can easily fix this annoying Java compilation error.

new java job roles

What Is Classpath?

Before we dive into the how and why of it, we need to understand what Classpath is and its role in Java.

The classpath is the file path where the JRE (Java runtime environment) searches for the classes and other resource files to run the code. As the name suggests, it is simply a file path where the .class format files can be found in a JDK package or directory.

Classpath can be set using two ways:

  1. Using the -classpath option at the time of executing the code,
  2. By setting the file path to the system CLASSPATH environment variable.

When the JVM is unable to locate the main class, it is usually because you would have entered the wrong .class name to run the classpath or the corresponding .class files have been altered.

See this example of generating a class file of a simple code:

1. public class example01 {
2. public static void main(String[] args) {
3. out.println("This is a simple code");
4. }
5. }

To run a .class file, you can use the following command:

java <.class filename>

Now if we run this line of code to run the class we made.

$ java eg01

Output:

Error: Could not find or load main class

It will fail with the error “Could not find or load main class eg01.”

As mentioned earlier, the .class file will have the same name given to the Java class of the program. So, in this case, the main class will have the name example01, not eg01.

Let’s try this one more with the correct name:

$ java example01

Output:

This is a simple code

Now it ran successfully.

While it’s not as simple in a project, the easiest way to rectify it is by either manually specifying the classpath or using packages.

Using Packages

Packages are used in Java to group similar classes or to provide a unique namespace to a group of classes. We will be now creating a class called example02 and place it in a package called example02Package.

1. package example02Package;
2. public class example02 {
3. public static void main(String args[]) {
4. out.println("File is found successfully!");
5. }
6. }

We will then use this package to visualize how the classpath works in Java. In your files directory, a package is represented as an independent folder by its name that you can easily observe in a file manager application.

After ensuring that the working directory is the same as the one that contains the package folder, you can also change the working directory on the terminal (command prompt) by using the cd command on almost every popular operating system.

Example02.java can be compiled by running the following command:

  1. package example02Package;
  2. javac example02Package/example02.java

This will now save the compiled .class file in the example02Package directory.

To run the compiled class, you need to type in the fully qualified class name in the command line. The fully qualified name of a Java class is written by prefixing it with the package name.

For this example, this is the fully qualified name:

java example02Package.example02

Using packages also allows Java developers to call executables from different packages from the same working directory. It can be easily done just by modifying the fully qualified class name without getting the ‘could not find or load main class error.

Manually Specifying Classpath

The other way to prevent could not find or load main class error is by manually specifying the classpath. It is recommended to manage your java files by creating separate directories for all source files and classes.

Just like the .class files are labeled as classes. the directory with source files is labeled as src. It also helps in significantly reducing the chances of JVM not being able to find the main class.

If you use this method for organization, this is how the directory structure of your projects will look like before compilation:

|___Project01
|     |___src
|           |___example02Package
|                       |___example02.java
|
|     |___classes

The indentations in the above example show the one level of the file hierarchy that your project should be following.

When compiling this code, you must make sure that your working directory is Project01. The following command is used to execute it:

javac -d classes src/testPackage/Test.java

The .class executable file must be saved in Project01/classes/example02Package. Now, the file directory structure will look like this:

|___Project01
|     |___src
|           |___example02Package
|                       |___example02.java
|
|         classes
|           |___example02Package
|                       |___example02.class

To run the .class file, you have to run the Java command with the fully qualified class name and the specification of the local classpath. Every path is declared relative to the working directory, which in this example is Project01.

java -classpath classes example02Package

Running this command will now provide you with the desired output without any chances of getting the error. The question is, why do you need to reorganize the files to solve such a small runtime error?

Why Organizing Files is important in Java?

The primary reason behind why the ‘could not find or load main class ’ is encountered is because JVM is unable to locate where your .class files were being saved.

The easiest way to resolve this error and prevent it from ever happening is to organize where the .class files are saved. Developers have to explicitly indicate the JVM to look for the .class file in the assigned location. This can only be done by organizing the source files and executables separately and using the working directory to manage either manually or using packages.

Your project code will likely keep expanding over time as the project work keeps going. By adding more constructs such as inheritance, inner classes, and more to your project, the file system keeps getting more complex. In such projects, this simple practice of organizing files can save you several hours of precious time that it would take in debugging if something goes wrong and you end up getting the could not find or load main class error.

See Also: What Are Annotations in Java – How do They Work

Conclusion

“Could not find or load main class” error is very common in Java however, we have discussed some effective ways to prevent this error in the article above. The file organization methods we discussed not only prevent this error but also make your code and directories manageable. These fixes go a long way for Java developers as they save a lot of time and trouble when debugging, especially for complex codes!

new Java jobs



author

admin


Candidate signup

Create a free profile and find your next great opportunity.

JOIN NOW

Employer signup

Sign up and find a perfect match for your team.

HIRE NOW

How it works

Xperti vets skilled professionals with its unique talent-matching process.

LET’S EXPLORE

Join our community

Connect and engage with technology enthusiasts.

CONNECT WITH US