Layers of Logic

1.2

JVM, JRE, JDK, and Your First Program

Install the tools, compile one Java file, and run it from the terminal.

Core20 min read4 exercises

Before you start

Topics used in this lesson

01

Prerequisites

Section 1.1 established the route from source code to a platform:

source -> bytecode -> JVM -> processor and operating system

This lesson makes that route concrete. You will create one file, compile it, inspect the result, and run it without an IDE.

02

Why this matters

Java installation instructions use three similar names: JVM, JRE, and JDK. Mixing them up creates practical confusion.

Someone installs a runtime and expects to find a compiler. Another person passes a .class file to javac. An IDE hides both mistakes until a terminal or server exposes them.

We need one accurate model, then one complete trip through the tools.

03

Core explanation

The three names

JDKJava Development Kit
  • javac compiler
  • java launcher
  • inspection and debugging tools
  • runtime modules

Install this when you develop Java programs.

JREJava Runtime Environment
  • JVM
  • standard runtime libraries
  • supporting runtime files

The traditional package for running Java applications.

JVMJava Virtual Machine
  • class loading
  • bytecode execution
  • managed memory

The engine defined by the JVM specification and implemented by a runtime.

The conceptual nesting is still useful. Modern JDKs may not ship a separate folder or download named JRE.

Use these working definitions:

  • The JVM executes class files under the JVM rules.
  • A runtime environment adds the libraries and files applications need.
  • The JDK adds tools used to build, inspect, and debug programs.

For this course, install a JDK. It contains what you need for both development and execution.

Install one

Get a build of OpenJDK version 21. It is a long-term support release, and every command in this course works on it. Version 17 also works if you already have it. Anything older is missing language features used later in the course.

Adoptium publishes free, well-maintained builds for all three operating systems. Download the installer for your machine, not a .zip or .tar.gz, and choose the package marked JDK. A package marked JRE gives you java without javac.

macOS

  1. Pick the right fileChoose the .pkg installer. Select aarch64 for Apple Silicon (M1 and later) or x64 for an Intel Mac. The Apple menu, then About This Mac, names your chip.
  2. Run the installerOpen the .pkg and accept the defaults. It installs the JDK and puts java and javac on your PATH for you.
  3. Open a new terminalOpen Terminal from Applications, then Utilities. A terminal that was already open will not see the new installation.

Windows

  1. Pick the right fileChoose the .msi installer for x64.
  2. Turn on the PATH optionsThe installer shows a screen of optional features. Set Add to PATH and Set JAVA_HOME variable to "Will be installed on local hard drive". This is the step that decides whether the next section works.
  3. Open a new terminalPress Start, type powershell, and open Windows PowerShell. A window that was already open still has the old PATH.

Linux

  1. Install from your package managerOn Debian or Ubuntu: sudo apt update then sudo apt install openjdk-21-jdk. On Fedora: sudo dnf install java-21-openjdk-devel.
  2. Install the right packageThe name matters. openjdk-21-jre and packages ending in -headless do not include javac.

Confirm the installation

Open a terminal and run:

java -version
javac -version

The first command checks the launcher. The second checks the compiler.

If java works but javac does not, you may have only a runtime on your command path. If neither works, the JDK may be missing or its bin directory may not be on PATH.

PATH is the list of directories your shell searches for commands. It is an operating-system feature, not a Java keyword.

Write a complete program

Create a plain text file named Demo.java:

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

Every part has a job:

public class Demo                 declares a class named Demo
public static void main(...)      declares the program entry method
String[] args                     receives command-line arguments
System.out.println(...)           prints one line

You do not need the full meaning of public, static, or String[] yet. Their later lessons will rebuild this line from known parts.

One rule matters now: a public top-level class named Demo belongs in Demo.java. Capitalisation must match.

Compile it

Change the terminal to the directory containing the file. Run:

javac Demo.java

If compilation succeeds, the command usually prints nothing. It creates Demo.class in the same directory.

You now have two different artifacts:

Demo.javaDemo.class
ContainsJava source textClass-file data and bytecode
Made forHumans and javacA compatible JVM
Typical actionEdit and compileLoad and execute

Run it

Run the class through the Java launcher:

java Demo

Expected output:

Hello, Java!

javac accepts a file name, including .java. The java launcher accepts a binary class name, so you write Demo without an extension.

04

Optional internals

Going deeper
Open optional internalsYou can finish the core lesson without this section.

Compile-time errors and run-time errors are different

Remove the semicolon after println, then run javac Demo.java. The compiler reports a source error and does not produce a new valid class file.

Restore the semicolon, compile, then run java Missing. The launcher cannot find a class with that name.

These failures occur at different stages:

Find the failing stage

  1. Source parsing and type checkingjavac reports invalid Java source.
  2. Class lookupThe launcher reports that the requested class cannot be found.
  3. Entry-point lookupThe class exists, but no suitable main method exists.
  4. Program executionThe program starts and may then throw an exception.

Reading the stage is faster than treating every red message as the same problem.

The class path answers “where?”

By default, java Demo searches the current directory for Demo.class. That search location is part of the class path.

You can state the current directory explicitly:

java -cp . Demo

-cp means class path. The dot means the current directory.

Packages later add directory structure and qualified names. The same lookup rule remains: the runtime needs both the class name and a place to search.

What the runtime does with the class

Before your first println executes, the runtime performs several jobs.

  1. Loading: locate the bytes for Demo and create the JVM’s in-memory representation.
  2. Verification: check structural and bytecode safety rules.
  3. Linking: prepare class data and resolve required symbolic references.
  4. Initialization: run required static initialization for the class.
  5. Invocation: call the valid main method.

That is why java Demo does more than open a file and read instructions from the first byte.

Interpreting and compiling at run time

javac is an ahead-of-time compiler from Java source to JVM bytecode. That makes Java a compiled language.

A common JVM can also interpret bytecode and compile selected methods into native code while the program runs. The second compiler is called a just-in-time compiler, or JIT compiler.

Runtime profiling lets the JVM spend optimisation effort where the program actually spends time. Code that rarely runs may not need expensive compilation.

These are implementation strategies. The JVM specification defines observable behaviour, not one mandatory balance between interpretation and JIT compilation.

You can inspect the class file without decoding raw bytes:

javap -c Demo

javap is a JDK tool. The -c option prints readable bytecode instructions for each method.

SE, EE, and ME describe platforms and ecosystems

Java SE is the standard Java platform and core API used throughout this course.

Jakarta EE, historically called Java EE, adds specifications for enterprise server applications. It builds on Java SE rather than replacing it.

Java ME targets constrained and embedded environments. Its role is much smaller in modern general-purpose Java development.

These labels are not three different Java languages. They describe platform profiles, APIs, and deployment environments around the language.

05

Tradeoffs

An IDE can compile and launch Java for you. That is useful after you understand the two commands it is running.

Learning the terminal route exposes file names, class names, stale class files, and class-path mistakes. Those details return in build tools, test runners, servers, and production diagnostics.

The small cost now prevents the development environment from becoming a black box.

06

Check yourself

Answer from memory before opening the explanation. Then compare your rule with the answer and revisit the example if they differ.

  1. You want to write, compile, and run Java programs. Which package do you install?

    Show the answer

    Install a JDK. It provides the Java runtime, the javac compiler, and development tools. A JVM is the execution engine. JRE is the traditional name for a JVM plus runtime libraries and supporting files.

  2. Why does compilation use "javac Demo.java", while execution uses "java Demo"?

    Show the answer

    javac compiles a source file, so it receives the file path. The launcher receives a binary class name. It searches the class path for Demo.class, loads that class, and calls its entry method.

  3. What happens before the first statement in main runs?

    Show the answer

    The launcher starts a JVM. The runtime locates and loads the class, checks its bytecode, links it, and initializes required class state. It then invokes a valid main method.

07

Exercises

Start with the warm-ups. Continue to practice exercises when the basic idea works. Challenges are optional: skip them on a first pass and return after the next lesson.

  1. Warm up: one idea with close guidance.
  2. Practice: combine the idea with earlier material.
  3. Challenge: investigate an edge case or implementation detail.

4 exercises60 pointsabout 36 minutes

Coding exercises provide starter code and concrete success checks. Exercises marked checked can also be run through the VS Code extension. Concept questions are selectable quizzes that give immediate feedback on this page.
A

Verify a JDK Installation

Warm up·5 min·10 points

quizex-1-2-a

Installing a package and having a working command-line toolchain are different outcomes. Select the observations that prove both the runtime and compiler are ready.

Which two command results together show that a Java 21 JDK is available from the terminal?

Select every answer that applies.

Why this exercise exists

This exercise checks whether you can verify that a complete JDK is available.

B

Your First Program, the Slow Way

Warm up·15 min·10 points

codingex-1-2-b

Write, compile and run a Java program using nothing but a text editor and a terminal.

Do not use the Run button in VS Code or IntelliJ for this one. Use it for the rest of your life if you want, but do this one by hand.

The reason is simple. That green button runs javac and then java for you and hides both. If you have never seen the two steps separately, then compiling and running are one mysterious action in your head, and every error message from either one will look the same to you.

Do it once by hand and they become two clearly different things forever.

Steps and success checks

  • Save the file as Demo.java, with a capital D
  • Compile it from the terminal, not with a Run button
  • Confirm that Demo.class appeared next to Demo.java
  • Run it from the terminal and see the output
Demo.java
public class Demo {
    public static void main(String[] args) {
        System.out.println("Hello from bytecode");
    }
}

Sample run

You type
javac Demo.java
java Demo
It prints
Hello from bytecode
Hint 1
Open a terminal and move into the folder that holds the file first. On Mac and Linux use cd. On Windows, Command Prompt uses cd too.
Hint 2
After javac Demo.java nothing is printed. That is success. A compiler that has nothing to complain about says nothing. Check with ls or dir that Demo.class exists.
Hint 3almost the answer
The run command is java Demo. Not java Demo.java, and not java Demo.class. The java command wants a class name, and it finds the file itself.
Why this exercise exists

This exercise checks whether you can run Java without an IDE hiding the steps.

C

Read Bytecode Output

Practice·8 min·20 points

quizex-1-2-c

The disassembler shows instructions such as ldc and invokevirtual, plus a constructor block. Select every conclusion supported by those details.

Which statements correctly interpret the output produced by javap -c?

Select every answer that applies.

Why this exercise exists

This exercise checks whether you can interpret the important parts of javap output.

D

Identify the Failing Tool

Practice·8 min·20 points

quizex-1-2-d

Two failures happen while compiling source code. Two happen only when the program is launched. Choose the row that puts each failure on the correct side.

Which row correctly identifies the tool that reports each failure?

Choose one answer.

Why this exercise exists

This exercise checks whether you can distinguish compiler failures from launcher failures.

08

Where this leads

You have completed the first full Java toolchain cycle:

Demo.java -> javac -> Demo.class -> java -> class loading -> main

The next phase begins with the values that move through those bytecode instructions.

Later connections

JVM is next used in 4.2 - How Arrays Work