Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today's three abbreviations sound alike, so let's sort them by size. Which one is the biggest?
The JDK? It's the one we installed so that we could write programs.
Right. The JDK is the Java Development Kit: a JRE plus development tools such as javac, the compiler, and javadoc, which builds documentation from the comments in your code.
Then what is inside the JRE?
The JVM, plus the core libraries: ready-made classes such as String, Math and System that every program uses while it runs.
Why is the JVM called a virtual machine?
Because it is not a physical chip. It is a program that behaves like a computer whose machine language is bytecode, so Java programs are written for that imagined computer instead of for your real processor.
So the JDK is the JRE plus tools, the JRE is the JVM plus libraries, and we write with the JDK but run with the JRE and the JVM inside it.
Suppose we save a program as Hello.java. What turns it into something a computer can run?
We compile it by typing javac Hello.java.
Yes, and javac comes from the JDK. It checks the code and produces Hello.class, which holds bytecode, not the machine code of any particular processor. If the code has a mistake, javac reports a compile error and creates no .class file at all.
And then java Hello runs it?
The java command starts the JVM. The JVM loads Hello.class, checks the bytecode and executes it, either interpreting it or compiling the busiest parts into this processor's own machine code as the program runs.
Where does System.out.println come from? We never wrote it ourselves.
From the core libraries in the JRE: your program borrows that ready-made class every time it runs. Notice that only the compile step needed the JDK; running Hello.class needs just the JRE and its JVM.
So on a computer without the JDK, I could still run Hello.class, but I could not change Hello.java and compile it again.
If I copy Hello.class from my Windows laptop to a Linux server, will it still run?
Yes, as long as the server has Java installed?
As long as it has a JVM. Every operating system gets its own JVM, built for that system, and all of them understand the same bytecode. That is the promise Write Once, Run Anywhere.
Can a program find out which JVM is running it?
It can ask with System.getProperty, using keys such as java.version, java.vm.name and os.name. It can even check for the compiler: ToolProvider.getSystemJavaCompiler() from the javax.tools package returns null on a runtime-only installation.
So my grandmother only needs a runtime to play my Java game, but I need the JDK to build it.
Exactly. Many Java downloads today come only as a JDK, which covers both jobs, but the roles of the three parts stay the same.
And whichever download we choose, the same .class file runs anywhere that has a JVM.
the development kit, made of a JRE plus development tools such as javac and javadoc.
the runtime environment, made of the JVM plus the core libraries; it is enough to run programs.
the virtual machine that loads, checks and executes bytecode on one particular system.
javac turns .java source code into .class files of bytecode, not into machine code.
the same .class file runs on any operating system that has its own JVM.
developers need the JDK, while people who only run programs need just a runtime.
The smallest complete program to compile with javac (a JDK tool) and run with java (which starts the JVM). The two comment lines show the commands you would type in a terminal; the code editor does both steps for you:
// Compile: javac Hello.java (the JDK's compiler creates Hello.class)
// Run: java Hello (the JVM loads Hello.class and runs it)
public class Hello {
public static void main(String[] args) {
System.out.println("Hello from the JVM!");
}
}Asking the running JVM to describe itself with System.getProperty. The output depends on the machine: for example, on a Linux machine with Java 13 it prints 13.0.1, OpenJDK 64-Bit Server VM and Linux. Other useful keys include os.arch, java.vendor, java.home (the runtime's folder) and java.class.version (the bytecode version it runs):
public class WhichJvm {
public static void main(String[] args) {
System.out.println("Java version : " + System.getProperty("java.version"));
System.out.println("JVM name : " + System.getProperty("java.vm.name"));
System.out.println("OS name : " + System.getProperty("os.name"));
}
}The core libraries at work: String, Math and LocalDate all ship with the JRE, so the program can use them without writing them. The date is 17 September 2019, the day Java 13 (the version this course's code editor runs) was released:
import java.time.LocalDate;
public class CoreLibraries {
public static void main(String[] args) {
String word = "bytecode";
System.out.println(word.toUpperCase()); // BYTECODE, from the String class
System.out.println(Math.sqrt(144)); // 12.0, from the Math class
LocalDate javaThirteen = LocalDate.of(2019, 9, 17); // Java 13 was released on this date
System.out.println("Java 13 came out on a " + javaThirteen.getDayOfWeek()); // TUESDAY
}
}The JVM hides platform details from your program. For example, the file separator is a forward slash on Linux and a backslash on Windows, and the same bytecode simply asks the JVM which one applies; the number of processors also depends on the machine:
public class PlatformDetails {
public static void main(String[] args) {
System.out.println("Operating system : " + System.getProperty("os.name"));
System.out.println("File separator : " + System.getProperty("file.separator"));
System.out.println("Processors : " + Runtime.getRuntime().availableProcessors());
}
}The JDK helps you make it, the JRE lets users take it, and the JVM is what wakes it.
A playwright writes a script at a desk full of tools: that desk is the JDK. The script is translated into a universal stage language, the bytecode in a .class file. Every theatre in the world, one for each operating system, has its own stage crew, the JVM, that can perform any script written in that language using the theatre's props, the core libraries. The audience needs only a theatre, the JRE, never the playwright's desk.
Develop, Run, Machine: the D in JDK is for developing, the R in JRE is for running, and the M in JVM is the machine that does the running.
W.O.R.A. - Write Once, Run Anywhere, the promise that the JVM keeps.
Term
What does JDK stand for, and what does it add to the JRE?
Click to flip
Term
Which two things make up the JRE?
Click to flip
Term
What does javac produce from Hello.java?
Click to flip
Term
Why can the same .class file run on both Windows and Linux?
Click to flip
Term
What does someone need just to run a Java program?
Click to flip
Term
What does ToolProvider.getSystemJavaCompiler() return on a runtime-only installation?
Click to flip
The kit for writing Java programs: a JRE plus development tools such as javac and javadoc.
Everything needed to run Java programs: the JVM plus the core libraries.
The engine that loads, checks and executes bytecode on a particular machine.
The Java compiler, a JDK tool that turns .java source files into .class files containing bytecode.
A JDK tool that generates HTML documentation from special comments in source code.
The platform-neutral instructions stored in .class files; each Java release has its own class-file version, for example 57.0 for Java 13.
The ready-made classes that come with the JRE, such as String, Math and System.
The ability of one compiled program to run on any operating system that has a JVM, summed up as Write Once, Run Anywhere.
JDK, JRE, and JVM are essential components of Java development and execution, each serving a crucial role in the process.
This section elaborates on the roles of JDK (Java Development Kit), JRE (Java Runtime Environment), and JVM (Java Virtual Machine) in Java programming. The JDK includes tools for writing and compiling Java applications, the JRE offers a runtime environment for executing them, and the JVM interprets the compiled code to ensure platform independence.
In this section, we explore three foundational elements of Java programming:
JDK (Java Development Kit): The JDK is a software development kit that provides tools necessary for developing Java applications. It includes the JRE as well as additional development tools such as javac for compiling code and javadoc for generating documentation.
JRE (Java Runtime Environment): The JRE is essential for running Java applications. It contains the JVM along with libraries that provide necessary functionalities for Java programs. The JRE allows developers to execute Java applications without the more complex tools included in the JDK.
JVM (Java Virtual Machine): The JVM is the core component that acts as an interpreter between the compiled Java bytecode and the underlying operating system. It allows Java applications to run on different platforms without modification, enabling the language’s famous slogan of "Write Once, Run Anywhere" (WORA).