Easy java core-java

What are the JDK, the JRE and the JVM, and how does Java code run?

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 class file 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, jfr and more.

How a program runs:

  1. javac Order.java compiles source code into Order.class containing bytecode.
  2. java Order starts the JVM, which loads, links and initialises the class through class loaders.
  3. 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.
  4. The garbage collector frees objects that are no longer reachable.
Sources (2)
  1. JVM Specification, chapter 1: Introductiondocs.oracle.com
  2. dev.java: Getting started with Javadev.java