Java · Basics

Installing the JDK

To write Java you need a JDK and an editor. This takes about ten minutes.

Beginner 5 min Lesson 2 of 31Examples target Java 21 LTS (they also run on Java 25).
Before you start:Introduction to Java
0/31 in Java
On this page · 5 min · Beginner

In simple words#

The JDK is your full workshop (tools + engine). JAVA_HOME is the signboard telling every other tool where the workshop is.

JDK distribution
Temurin, Oracle, Microsoft Build of OpenJDK — all Java.
JAVA_HOME
Environment variable pointing to the JDK folder.
jshell
Interactive Java console for quick experiments.

Learning objectives#

  • Install a JDK
  • Set JAVA_HOME and PATH
  • Verify the installation
  • Run code in jshell and an IDE

Explanation#

Download a JDK 21 or 25 LTS build (e.g. Eclipse Temurin from adoptium.net) or install with a package manager.

The installer usually sets PATH; set JAVA_HOME to the JDK folder for build tools like Maven and Gradle.

java -version and javac -version confirm the install.

IntelliJ IDEA Community (free) is the most popular Java IDE; VS Code with the Extension Pack for Java also works well.

Code example: Install and verify#

ShellInstall and verify Illustrative
# Windows (winget)
winget install EclipseAdoptium.Temurin.21.JDK


# macOS (Homebrew)       brew install --cask temurin@21
# Ubuntu                 sudo apt install openjdk-21-jdk


java -version
javac -version
echo $JAVA_HOME          # Windows PowerShell: $env:JAVA_HOME


jshell
jshell> 2 + 3 * 4
jshell> "Java".repeat(3)
jshell> /exit
Expected output
openjdk version "21.0.5" 2024-10-15 LTS
javac 21.0.5
$1 ==> 14
$2 ==> "JavaJavaJava"

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. A package manager installs the JDK.
  2. java/javac -version confirm the tools are on PATH.
  3. JAVA_HOME is used by Maven and Gradle.
  4. jshell evaluates Java expressions instantly.

Real-world use case#

New team members install the same JDK version so builds behave identically on every laptop.

Common mistakes#

Installing only a JRE

You need the JDK to compile code.

Several JDKs and a wrong PATH

Check which java runs: where java (Windows) / which java (macOS/Linux).

Best practices#

  • Use an LTS version
  • Set JAVA_HOME
  • Keep one default JDK
  • Use jshell for quick experiments

Remember this#

Install a JDK, not just a JRE.

java -version / javac -version.

jshell = Java playground.

Practice exercise#

Your task: Use jshell to calculate how many seconds are in a day.

Show solution
Text Illustrative
jshell> 24 * 60 * 60
$1 ==> 86400

Mini quiz#

Installing the JDK quizMultiple choice Question 1 of 1

Which variable do Maven and Gradle use to find the JDK?

Score so far: 0/0

Want more? Practise all Java MCQs · Java interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.