Java – error: could not find or load main class | Solutions

There are 3 common reasons why your main class wasn’t found by Java, let’s explore them all:

  1. Incorrect class name argument
  2. Misconfiguration in the application classpath
  3. 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 com.billing.invoices.ShowInvoice

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

java com/billing/invoices/showinvoice.class  

is the not correct form of the path since the file name is actually ShowInvoice and not showinvoice

Check if there’s a typo in path

java com/billing/invoices/SbowInvoice.class  

Notice how 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.