What Java is and where it runs

Java is a compiled, statically typed, object-oriented language with one promise that has held for thirty years: a program built once runs on any machine that has a Java runtime. The compiler turns the source into bytecode, a compact instruction set for a machine that does not exist as hardware, and a Java virtual machine (JVM) runs that bytecode on the machine you actually have.

This lesson is the tour before the work: one program built and run, the bytecode it becomes, the tools that make up a JDK, the platforms a JVM exists for, and the second way to run a single source file without a build step. Every command and every output line on this page was run on the machine named at the end of the lesson.

A first program

Save this as Hello.java. The name matters, and the next lesson explains why: javac refuses a public class whose file has another name.

public class Hello { public static void main(String[] args) { System.out.println("Hello, Java"); } }

javac -Xlint:all Hello.java
java Hello

Hello, Java

The first command writes Hello.class next to the source and prints nothing. The second asks the JVM to load the class named Hello and call its main method, and that method writes one line.

-Xlint:all is not needed to compile this file. It turns on every warning the compiler has, and every lesson of this course builds with it, so a warning is a thing you see rather than a thing you meet later on somebody else's machine. A clean build with it prints nothing, as the output above shows.

From source to bytecode

The class file is not machine code for x86 or ARM. Its first four bytes are the same in every class file, the number 0xCAFEBABE, followed by two version numbers. The version 65.0 in the header means Java 21: each release of the language raises that number, and a JVM refuses a class file newer than itself, which is the one compatibility rule that matters when a build says UnsupportedClassVersionError.

od -An -tx1 -N8 Hello.class

ca fe ba be 00 00 00 41

The last two bytes are the class file version, 0x41, which is 65 decimal. The JVM reads this file, verifies that the bytecode does not do anything the language forbids, and only then runs it. The tool that prints the bytecode as instructions is javap, and lesson 003 uses it on a program of its own.

The JDK and the JRE

A Java runtime is two different products with almost the same name. The JRE, the Java Runtime Environment, holds the JVM and the standard library, and it is enough to run a program. The JDK, the Java Development Kit, holds the JRE plus the compiler and the tools, and it is what you install to write a program.

java -version
javac -version

openjdk version "21.0.13.1" 2026-11-17
OpenJDK Runtime Environment (build 21.0.13.1+2-alpine-r0)
OpenJDK 64-Bit Server VM (build 21.0.13.1+2-alpine-r0, mixed mode, sharing)
javac 21.0.13.1

Two names, one installation: java runs and javac compiles, and both report the same version. OpenJDK is the reference implementation; the vendor string on this machine is Alpine, because the package comes from Alpine Linux. A JDK from another vendor reports its own vendor string and compiles the same programs, and the install guide names the package for each system.

This course uses Java 21, the long-term-support release. Records, sealed types, pattern matching for switch, text blocks and virtual threads are final in 21, and every lesson may use them. Preview features are not used anywhere, so nothing needs --enable-preview.

Where Java runs

A JVM exists for desktop and server systems, for phones, for smart cards and for the larger embedded boards. The program below asks the runtime what it is running on. The values come from the JVM, not from a compile-time constant, so the same class file prints different lines on another machine.

The file Platform.java:

public class Platform { public static void main(String[] args) { System.out.println("version: " + System.getProperty("java.version")); System.out.println("vendor: " + System.getProperty("java.vendor")); System.out.println("vm: " + System.getProperty("java.vm.name")); System.out.println("os: " + System.getProperty("os.name")); System.out.println("arch: " + System.getProperty("os.arch")); System.out.println("runtime: " + Runtime.version()); } }

javac -Xlint:all Platform.java
java Platform

version: 21.0.13.1
vendor: Alpine
vm: OpenJDK 64-Bit Server VM
os: Linux
arch: amd64
runtime: 21.0.13.1+2-alpine-r0

System.getProperty reads a property the JVM sets at startup, and Runtime.version() returns the version as an object with its parts. The next lesson takes the version apart, and the Java basics page covers the rest of the syntax in one place.

The source launcher

Since Java 11 the java command takes a source file directly. It compiles the file in memory and runs the first class with a main method, without writing a class file you can see:

java Hello.java

Hello, Java

The source launcher is convenient for one file and a quick test, and it hides the reason this course starts with javac: the compiler is a separate step that you will need the moment a program has two files, a package or a library. There is also a REPL, jshell, for a line or two of Java without a file at all; lesson 007 uses it and says why every other lesson still writes a file.

The run page collects the commands of this lesson in one place, and the install guide puts a JDK on your machine before lesson 002 checks the version.

Exercises

  1. Change the string in Hello.java and build it again. Which file changed, and which one did javac rewrite?
  2. Run java Hello in a folder where Hello.class was deleted. What does the JVM say?
  3. Add a second println to Platform.java and predict the line before you run it.
  4. Build Hello.java with javac Hello.java and then with javac -Xlint:all Hello.java. Compare what the two commands print.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does javac write for a source file?

    Answer

    A class file with bytecode

    javac compiles the source to a class file, and the JVM runs the bytecode inside it.

  2. What runs the bytecode of a class file?

    Answer

    The JVM

    The Java virtual machine loads, verifies and runs the bytecode; javac is needed only to produce it.

  3. Which command compiles Hello.java with every warning turned on?

    Answer

    javac -Xlint:all Hello.java

    javac is the compiler, and -Xlint:all enables all of its warning groups.

  4. What does this program print? public class Q1 { public static void main(String[] args) { System.out.println("Hello, Java"); } }

    Answer

    Hello, Java

    println writes the string exactly as it is written, with its capital letters.

  5. What does this program print? public class Q2 { public static void main(String[] args) { System.out.println("first"); System.out.println("second"); } }

    Answer

    first / second

    Each println ends its line, so the program writes two lines.

  6. What does java Hello.java do that javac Hello.java does not?

    Answer

    It compiles the file in memory and runs its main method

    The source launcher compiles a single file and runs it without leaving a class file behind.

  7. Which of these belongs to the JDK but not to the JRE?

    Answer

    javac

    The JRE runs programs; the JDK adds the compiler and the development tools.

  8. What does this program print on a Java 21 runtime? public class Q3 { public static void main(String[] args) { System.out.println("feature: " + Runtime.version().feature()); } }

    Answer

    feature: 21

    feature() returns the major version, 21; the build and patch numbers are the other parts of the version.

  9. A file named hello.java holds public class Hello. What does javac print?

    Answer

    error: class Hello is public, should be declared in a file named Hello.java

    A public class must live in a file named after it, and javac says exactly that and writes no class file.

  10. What is the first line of the compiler output for this program? public class Q4 { public static void main(String[] args) { System.out.Println("hi"); } }

    Answer

    error: cannot find symbol

    Java is case-sensitive: Println is not a method of PrintStream, and javac reports an unknown symbol.

The code on this page was built with OpenJDK 21.0.13 (javac and java) and run on Alpine Linux in a Docker container.

Article author: Arthur Isaev