The 30 second answer
The JVM is the virtual machine that runs Java bytecode. The JRE was the JVM plus the standard libraries, which is what you needed to run an app, and the JDK adds the tools to develop: the javac compiler, jar, jshell, debugging and monitoring tools. javac compiles my source into bytecode in class files, the JVM loads those classes, interprets the bytecode at first and then the JIT compiler turns hot methods into native machine code. That is why the same jar runs on any operating system that has a JVM.
<!-- level: 1 --> <!-- must_know: true -->
- JVM (Java Virtual Machine): an abstract machine defined by the JVM Specification. It knows nothing about the Java language, only about the
classfile format, which holds bytecode and a symbol table. Each operating system has its own JVM implementation, so the same bytecode runs everywhere. - JRE (Java Runtime Environment): the JVM plus the Java class libraries, enough to run programs. Today you usually install a JDK, or build a trimmed runtime for your app with
jlink. - JDK (Java Development Kit): the runtime plus development tools:
javac,java,jar,jshell,javadoc,jcmd,jstack,jfrand more.
How a program runs:
javac Order.javacompiles source code intoOrder.classcontaining bytecode.java Orderstarts the JVM, which loads, links and initialises the class through class loaders.- The JVM interprets bytecode at first and profiles it. Methods that run often are compiled by the JIT (just-in-time) compiler into optimised native code.
- The garbage collector frees objects that are no longer reachable.
Sources (2)
- JVM Specification, chapter 1: Introductiondocs.oracle.com
- dev.java: Getting started with Javadev.java