Tutorials / Java: From Zero to Advanced / Installing JDK 21 and running Java code

Installing JDK 21 and running Java code

11 min read bash
Course contents (20 lessons)
  1. Getting started
  2. Installing JDK 21 and running Java code
  3. Your first program: how a Java file is built
  4. Language basics
  5. Variables and data types
  6. Strings, formatting and text blocks
  7. Control flow: if, switch expressions and loops
  8. Data and methods
  9. Arrays and the collections framework
  10. Methods, overloading and varargs
  11. Object-oriented Java
  12. Classes, objects and encapsulation
  13. Inheritance, abstract classes and interfaces
  14. Records, enums and sealed types
  15. Writing robust code
  16. Exceptions: checked, unchecked and try-with-resources
  17. Generics, bounded types and wildcards
  18. Files and the NIO.2 API
  19. Modern Java
  20. Lambdas and the Stream API
  21. Optional: handling missing values
  22. Concurrency: executors, CompletableFuture and virtual threads
  23. Tools, services and the final project
  24. Maven and Gradle basics
  25. Testing with JUnit 5
  26. A small REST service with the JDK HTTP server
  27. Final project: a library manager

Java, the JVM and the JDK

Java source code is compiled to bytecode. The bytecode runs on the Java Virtual Machine, the JVM. The same compiled program runs on Windows, Linux and macOS because each of them has a JVM. This is the idea behind the old slogan "write once, run anywhere", and it is mostly true.

To write Java you need the JDK, the Java Development Kit. It contains the compiler (javac), the launcher (java), the standard library and tools such as jshell and jar. The JRE, which is the runtime alone, is no longer shipped as a separate download in modern versions. You install the JDK and you have everything.

Why JDK 21

Oracle releases a new Java version every six months. Most are supported for only half a year. Every few years one is marked LTS, long-term support, and vendors support it for years. JDK 21, released in September 2023, is an LTS release. It includes records, sealed classes, pattern matching for switch and virtual threads as finished features. Those are the tools this course uses.

Installing

There are several builds of the JDK from different vendors: Eclipse Temurin, Oracle JDK, Amazon Corretto, Microsoft Build of OpenJDK and others. They are all based on the same OpenJDK source code. For learning, any of them is fine. I suggest Eclipse Temurin because it is free and has installers for every system.

  • Windows: download the Temurin 21 installer from adoptium.net and run it. Tick the option to set JAVA_HOME and update the PATH. Or run winget install EclipseAdoptium.Temurin.21.JDK.
  • macOS: download the package, or run brew install --cask temurin@21.
  • Linux: use your package manager, for example sudo apt install openjdk-21-jdk on recent Ubuntu or Debian releases. Package names differ between distributions, so check yours.

If you work with several Java versions later, SDKMAN (on Linux and macOS) lets you switch between them with one command.

Checking the install

Open a new terminal, because old terminals do not see the new PATH, and run:

java -version
javac -version

You should see something like openjdk version "21.0.x" from the first command and javac 21.0.x from the second. If java works and javac is not found, you have a runtime without the compiler, so install the JDK. If neither works, the PATH is wrong. On Windows, check that the JDK's bin folder is in your PATH. Many tools also use the JAVA_HOME variable, which should point to the JDK folder, not to its bin folder.

Three ways to run code

Create a folder and a file called Hello.java with this content:

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

1. Single-file launch. Since Java 11 you can run a source file directly:

java Hello.java

Java compiles it in memory and runs it. This is perfect for small experiments, and it is what I use throughout this course.

2. Compile, then run. This is the classic way and it is what build tools do for you later:

javac Hello.java
java Hello

The first command creates Hello.class, which contains bytecode. The second starts the JVM and runs that class. Note that you write Hello in the second command, not Hello.class.

3. jshell. The JDK includes an interactive shell. Type jshell, and then type Java expressions one at a time:

jshell
jshell> 2 + 3
$1 ==> 5
jshell> "java".toUpperCase()
$2 ==> "JAVA"
jshell> /exit

jshell is excellent for checking how a method behaves without creating a file.

An editor

You can start with any text editor. When you want more help, the common choices are IntelliJ IDEA Community Edition, which is free and what most Java developers use, Visual Studio Code with the Extension Pack for Java, and Eclipse. An IDE shows errors as you type, completes names and runs the debugger. For the first lessons, I still suggest typing in a plain editor and using the terminal, so that you see what the tools do.

Common mistakes

  • Opening the old terminal after installing. It still has the old PATH.
  • Having an older Java first on the PATH. If java -version shows 8 or 11, another install comes first. Remove it or reorder the PATH.
  • Naming the file differently from the public class. With javac, a public class Hello must live in Hello.java.
  • Writing java Hello.class. Leave off the extension.

Try it yourself

  1. Install JDK 21 and run both version commands.
  2. Create Hello.java and run it with java Hello.java.
  3. Compile it with javac, look at the folder and find the .class file, then run it with java Hello.
  4. Open jshell and calculate 365 * 24.
bash
java -version
javac -version

This course is free to read. Create a free account to enrol, save your progress and pick up where you left off.

Sign up free I already have an account