. Advertisement .
..3..
. Advertisement .
..4..
When you try to complete your task, you easily get “error: could not find or load main class” problem. This error is one of the most popular errors any programmer will make. So, why does it appear, and how can it be resolved? We’ll go over it with you.
When Do You Get The Error “error: could not find or load main class”?
You are trying to run Java class MyClass.java:
public class MyClass {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Now, the following command line is used to compile the aforementioned class:
$ javac MyClass.java
An executable .class file is produced by the compiler for MyClass:
$ ls
MyClass.class MyClass.java
Now, the “Could not find or load main class” warning is displayed if the Java command is used to execute the.class file with an erroneous name:
$ java Myclass
Error: Could not find or load main class Myclass
You have got “Could not find or load main class helloworld” error and your program did not work because the Java class in the program will have the exact same name as the.class file created by the compiler. Therefore, in your example, the primary class will be called HelloWorld rather than just HelloWorld.
The program will run properly if the proper name is specified:
$ java MyClass
Hello World
How To Solve The Error “error: could not find or load main class”
Solution 1: Utilize the exact class name
The simplest solution for the “error: could not find or load main class” problem is utilizing the exact class name. When running the program, you have to make sure that the class name’s spelling and casing are examined carefully.
Solution 2: Define what the classpath is
Excepting the method mentioned above, there is another method for you to solve your problem. Let’s follow these steps.
First you need to define what the classpath is before continuing. It is the collection of classes that are available to your JVM at the moment.
You instruct the JVM where to locate the.class files on the file system by using the classpath variable.
By using the -classpath option, you may specify the classpath while you are running a program:
java -classpath /my_programs/compiled_classes HelloWorld
Java will now search for the HelloWorld.class file in the arbitrary-named subdirectory /my programs/compiled classes. The classpath variable’s value is always “.” by default, which stands for the current directory. You have modified your directory to execute your program in the part above. What if you want to run it from a different folder? The classpath variable comes in handy at that point. You can simply say that your classpath is two directories up and one for every package part, and what you want to start your program from the directory com/ittutoria (which you have created) is:
$ java -claspath ../../ com.ittutoria.HelloWorld
Hello world..!!!
In this case, “..” stands for the parent directory. In your situation, the top of our package hierarchy is represented by “../../”.
Solution 3: Utilize cd.. back command
Let’s utilize the cd.. back command to return to the main directory if the class is located inside a package. For instance, the java class is located in the path below.
E:\primary\com\projects
Com.projects will be the class’ whole name. Program 1 should then use the cd.. back command to navigate to the primary parent directory.
E:\primary
java com.projects.Program1
After doing that, your error will be completely resolved and your program will run well without any errors. So, let’s apply this method for your problem to get your desired results.
Conclusion
The solution mentioned above is the best option for those still confused with this error “error: could not find or load main class“. We believe that, with this knowledge you can easily solve your problem. If you need our support or have other questions, we have a thriving community where everyone is always willing to help. Finally, we wish you a productive day filled with new solutions and code. Thank you for your reading!
Read more
→ Tips On Fixing The Error “Error: Could not find or load main class cucumber.cli.Main”
Leave a comment