Installing JDK 21 and running Java code
Course contents (20 lessons)
- Getting started
- Installing JDK 21 and running Java code
- Your first program: how a Java file is built
- Language basics
- Variables and data types
- Strings, formatting and text blocks
- Control flow: if, switch expressions and loops
- Data and methods
- Arrays and the collections framework
- Methods, overloading and varargs
- Object-oriented Java
- Classes, objects and encapsulation
- Inheritance, abstract classes and interfaces
- Records, enums and sealed types
- Writing robust code
- Exceptions: checked, unchecked and try-with-resources
- Generics, bounded types and wildcards
- Files and the NIO.2 API
- Modern Java
- Lambdas and the Stream API
- Optional: handling missing values
- Concurrency: executors, CompletableFuture and virtual threads
- Tools, services and the final project
- Maven and Gradle basics
- Testing with JUnit 5
- A small REST service with the JDK HTTP server
- 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.netand run it. Tick the option to setJAVA_HOMEand update the PATH. Or runwinget 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-jdkon 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 -versionshows 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 classHellomust live inHello.java. - Writing
java Hello.class. Leave off the extension.
Try it yourself
- Install JDK 21 and run both version commands.
- Create
Hello.javaand run it withjava Hello.java. - Compile it with
javac, look at the folder and find the.classfile, then run it withjava Hello. - Open jshell and calculate
365 * 24.
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