There are 3 common reasons why your main class wasn’t found by Java, let’s explore them all:
- Incorrect class name argument
- Misconfiguration in the application classpath
- Declaration of the class is in another package
Incorrect classname argument
This is the most common problem, check if you have used the right class name in the right form. Let’s go over this with a specific example with a class named
ShowInvoice and explore all common mistakes together.
Check if correct package is used
If the class was defined in the package
com.billing.invoices, then you must include the package name in the java command as well as
Make sure the path and file name is correct
java ShowInvoice.class java com/billing/invoices/ShowInvoice.class
See if class name casing is correct
is the not correct form of the path since the file name is actually
ShowInvoice and not
Check if there’s a typo in path
SbowInvoice is not correct because of typo.
Misconfiguration in the application classpath
This is 2nd most likely cause of the
could not find or load main class error. If you have time, I’d recommend going through documentation of java command and classpath guide provided by Oracle. I know you are busy now to read both, promise me you’ll bookmark them and I’ll list down the common problems software developers encounter because of this issue:
- Make sure the environment where your cli command gets executed has correct CLASSPATH variable set with correct directory and JAR file names.
- Check if relative paths in CLASSPATH variable are being resolved correctly.
- See if the class you are using can be located on effective CLASSPATH.
- Make sure the OS is not a problem, keep in mind that Linux, Mac and Windows all use different CLASSPATH separator. If they aren’t separated correctly then the classes won’t be loaded properly.
Problem still persists?
Above are all the common solutions of error: could not find or load main class. However, there are tons of other reasons why you are getting this error. If you tried everything above and are still getting it, then I’d recommend to add -Xdiag option to java command and try again.