- Could not find or load main class error usually appears when developing Java applications.
- To fix the problem, you need to ensure that your class names are correct.
- Also, you can also fix the issue by checking if your package names are correct.
- If you’re getting Could not find or load main class in Eclipse, check your class references.
Could not find or load main class in a programming error, and it usually appears when you write your applications in Java.
Fixing this issue might be hard, especially if you’re new to Java, but hopefully this guide should be able to help you with this problem.
How can I fix could not find or load main class?
1. Check the class name
- Open your Java program and check how you spelled the class name.
- Look at the compiler to see if the class matches.
- The class name typed on the compiler has to be an exact match with what you typed in the program.
Note: The most common mistake is when you declare a class name in the wrong format when running it from the compiler.
Also, the correct command to run a .class file generated by the Java compiler is
java <class filename> You don’t have to provide the .class extension: java HelloWorld.class.
This is is an obvious solution, but it might work if you’re getting Could not find or load main class in Eclipse.
2. Check the package name
- Check the package name where you have included your class.
- Use the correct form to run the class: Java packagename.classname. In our example, it will be dto.HelloHP.
- In the compiler, run the class from the parent folder of the package and not from the folder where the class is.
Note: If your class is included in a package, then to run it you must provide its fully qualified name.
Also, when you create a package, you actually create a folder structure like
After checking your code, the Could not find or load main class Java message should be gone.
3. Remove class references
- Go to the Run configurations in Eclipse.
- Click the Green Play Button and select Run Configurations.
- Remove all references to the class.
If you go now to Run As and select Java application, Eclipse will write a new configuration for the class you moved. Therefore, it won’t display the error anymore.
These are some simple guidelines, and they should come in handy if you encounter Could not find or load main class in Eclipse.
Troubleshooting and debugging is an important part of any Java application you create. This guide provides you solutions for a common error you might meet while running a java class.
If you run it from a Linux terminal and encounter this error, the first two solutions will work for you too. Check the class name and the directory from where you run your program.