Troubleshooting the java command

suggest change

This example covers common errors with using the ‘java’ command.

“Command not found”

If you get an error message like:

java: command not found

when trying to run the java command, this means that there is no java command on your shell’s command search path. The cause could be:

Refer to “Installing Java” for the steps that you need to take.

“Could not find or load main class”

This error message is output by the java command if it has been unable to find / load the entry-point class that you have specified. In general terms, there are three broad reasons that this can happen:

Here is a procedure to diagnose and solve the problem:

  1. Find out the full name of the entry-point class.
- If you have source code for a class, then the full name consists of the package name and the simple class name.  The instance the "Main" class is declared in the package "com.example.myapp" then its full name is "com.example.myapp.Main".
- If you have a compiled class file, you can find the class name by running `javap` on it.
- If the class file is in a directory, you can infer the full class name from the directory names.
- If the class file is in a JAR or ZIP file, you can infer the full class name from the file path in the JAR or ZIP file.
  1. Look at the error message from the java command. The message should end with the full class name that java is trying to use.
- Check that it exactly matches the full classname for the entry-point class.
- It should not end with ".java" or ".class".
- It should not contain slashes or any other character that is not legal in a Java identifier<sup>1</sup>.
- The casing of the name should exactly match the full class name.
  1. If you are using the correct classname, make sure that the class is actually on the classpath:
- Work out the pathname that the classname maps to; see http://stackoverflow.com/documentation/java/3720/classpath/19816/mapping-classnames-to-pathnames#t=201609101442436616455
- Work out what the classpath is; see this example: http://stackoverflow.com/documentation/java/3720/classpath/12852/different-ways-to-specify-the-classpath
- Look at each of the JAR and ZIP files on the classpath to see if they contain a class with the required pathname.
- Look at each directory to see if the pathname resolves to a file within the directory.

If checking the classpath by hand did not find the issue, you could add the -Xdiag and -XshowSettings options. The former lists all classes that are loaded, and the latter prints out settings that include the effective classpath for the JVM.

Finally, there are some obscure causes for this problem:

“Main method not found in class <name>”

This problem happens when the java command is able to find and load the class that you nominated, but is then unable to find an entry-point method.

There are three possible explanations:

Other Resources


1 - From Java 8 and later, the java command will helpfully map a filename separator (”/” or ““) to a period (”.”). However, this behavior is not documented in the manual pages.

2 - A really obscure case is if you copy-and-paste a command from a formatted document where the text editor has used a “long hyphen” instead of a regular hyphen.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents