Java Virtual Machine

Running the java command launches the Java Virtual Machine (JVM).
The JVM interprets bytecode - the binary instructions created by the Java Compiler and stored in a .class file.
bytecode¶
Platform-neutral instructions interpreted by the JVM at runtime.
Bytecode is not specific to any computing platform.
-
A Java program compiled on a Mac will run unchanged on Windows, Linux, or any other system with a standard JVM - hence Java's motto: Write Once, Run Anywhere.
-
It's the JVM itself that must be customized to each hardware/operating-system environment, not your Java code.
Bytecode consists of numeric instructions that access subroutines built into the JVM.
- The
.classfile also includes all the literal data values from your code, the names of all the classes your code uses, line number information for printing error messages, and other information needed by the JVM.
The JVM has many responsibilities including loading classes your application uses and detecting errors (exceptions).
- One major responsibility of the JVM is managing memory, cleaning up unused data as needed.
You must give java the name of a class containing a properly-defined main method:
public static void main(String[] args) { /* ... */ }
-
The class name you give on the
javacommand line must correspond to a.classfile of the same name. -
If the
.classfile isn't found, or if it doesn't contain a propermain, an Error occurs and the JVM exits.
Java begins executing main, loading additional classes as needed.
The behavior of the JVM is defined by the Java Virtual Machine Specification.
-
The Sun/Oracle JVM, named HotSpot, is by far the most-used.
-
Anyone can write their own JVM; as long as it conforms to the JVM Specification it will be able to run any bytecode.