Lagos, Nigeria

[Fix] Could not find or load main class error in Java

How can I fix could not find or load main class?

1. Check the class name

check class name could not find or load main class
  1. Open your Java program and check how you spelled the class name.
  2. Look at the compiler to see if the class matches.
  3. 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 package name could not find or load main class
  1. Check the package name where you have included your class.
  2. Use the correct form to run the class: Java packagename.classname. In our example, it will be dto.HelloHP.
  3. 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

code could not find or load main class
  1. Go to the Run configurations in Eclipse.
  2. Click the Green Play Button and select Run Configurations.
  3. 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.

This post was written by Vlad Turiceanu and was first posted to WindowsReport

Do you find this article helpful? Your Friend might too. So, please Share it with them using the Share button above.
Will you like to get notified when I post new updates? Then Follow me on any of my social media handles: Google News, Telegram, WhatsApp, Twitter, Facebook, Pinterest.
You can also drop your email address below if you wish to be notified by mail.

[newsletter_form type="minimal" lists="undefined" button_color="undefined"]

Tags: , ,