Inheritance
Getting started with Java
Streams
Exceptions
Collections
Lambda Expressions
Generics
File I/O
Arrays
Interfaces
Maps
Strings
InputStreams and OutputStreams
Default Methods
Classes and Objects
Basic Control structures
Concurrent Programming Threads
Console I/O
Singletons
Visibility controlling access to members of a class
Regular Expressions
Autoboxing
Documenting Java Code
Executor, ExecutorService and Thread pools
Object Class Methods and Constructor
JAXB
Primitive Data Types
Networking
Optional
Enums
HttpURLConnection
Annotations
Audio
Data Class
Calendar and its Subclasses
Nashorn JavaScript engine
Java Native Interface (JNI)
Remote Method Invocation (RMI)
Iterator and Iterable
Operators
Asserting
Scanner
Properties Class
Preferences
Reflection API
Constructors
ByteBuffer
Serialization
JSON in Java
Random Number Generation
Recursion
Polymorphism
StringBuilder
Refernce Data Types
Bit Manipulation
Java Agents
Encapsulation
Type Conversion
BigInteger
BigDecimal
RSA Encryption
Varargs Variable Arguments
ThreadLocal
Logging
static keyword
Disassembling and Decompling
Resources on classpath
log4j, log4j2
JVM Flags
Oracle Official Code Standard
Character encoding
Java Memory Management
Immutable Objects
Object Cloning
Alternative Collections
Lists
BufferedWriter
LocalTime
Sets
Comparable and Comparator
JVM Tool Interface
Nested and Inner Classes
Apache Commons Lang
Getters and Setter
The Classpath
Bytecode Modification
XML Parsing using JAXP
Reference Types
Localization and Internationalization
JAX-WS
XML XPath Evaluation
Performance Tuning
Parallel programming with ForkJoin framework
Common Pitfalls
Non-Access Modifiers
Java Compiler
XJC
Installing Java Standard Edition
Process
Command line argument processing
Dates and Time using java.time
Fluent Interface
XOM - XML Object Model
Just in Time JIT compiler
FTP File Transfer Protocol
Java Native Access JNI
Modules
Pitfalls - exceptions
Pitfalls - language syntax
ServiceLoader
ClasssLoader
Object References
Pitfalss - performance
Creating Images Programmatically
Applets
NIO Networking
New File I/O
Secure objects
Pitfalls - threads and concurrency
Splitting a string into fixed length parts
Pitfalls - nulls and NullPointerException
SecurityManager
JNDI
super keyword
java.util.Objects class
Java command: java and javaw
The Java Command - java and javaw
Entry point classes
Running an executable JAR file
Running a Java applications via a main class
Troubleshooting the java command
“Command not found”
“Could not find or load main class”
“Main method not found in class <name>”
Other Resources
Running a Java application with library dependencies
Spaces and other special characters in arguments
Java Options
Atomic types
Floating Point Operations
Converting to and from strings
sun.misc.Unsafe
Java Memory Model
Java deployment
Java plugin system implementations
Queues and deques
Runtime commands
NumberFormat
Security and Cryptograhy
Java Virtual Machine JVM
Unit Testing
JavaBean
Expressions
Literals
Java SE 8 Features
Java SE 7 Features
Packages
Concurrency and Money
Concurrent Collections
Using ThreadPoolExecutor in multi-threaded applications
Java Editions, versions, releases and distributions
Dynamic method dispatch
JMX
Security and cryptography
Generating Java Code
JShell
Benchmarks
Collection Factory Methods
Multi-release JAR files
Stack-Walking API
TreeMap and TreeSet
Sockets
Java Sockets
Using other scripting languages in Java
Functional interfaces
List vs. set
2D graphics
Reflection
Deque interface
Enum Map
EnumSet class
Local Inner class
Java Print Service
Immutable class
String Tokenizer
File upload to S3 / AWS
Readers and writers
Hashtable
Enum starting with a number
SortedMap
WeakHashMap
LinkedHashMap
StringBuffer
Choosing Collections
C++ Comparison
CompletableFuture
Contributors

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
11 Maps
26 JAXB
30 Enums
33 Audio
76 Lists
79 Sets
90 JAX-WS
97 XJC
107 Modules
115 Applets
123 JNDI
151 JMX
154 JShell
160 Sockets