☕ Java

Java Installation

Before you write a single line of Java, you need the JDK installed and working. Here's how to install Java correctly on Windows, macOS, and Linux — and how to verify everything is set up before you write your first program.

JDK vs JRE — What to Install

Before downloading anything, understand what you actually need: - JRE (Java Runtime Environment) — Runs Java programs. No compiler. Use this if you only need to run existing Java applications. - JDK (Java Development Kit) — Includes the JRE plus the compiler (javac), debugger, and development tools. Install this if you're writing Java code. As a developer, always install the JDK. The JRE alone won't let you compile. Which version? Java 21 is the current LTS (Long-Term Support) release as of 2023 — the right choice for new projects. LTS versions receive security updates for years; non-LTS versions are supported for only 6 months.

Installing on Windows

Two options: the official Oracle installer, or a package manager. The package manager route is faster and easier to update. Option A — Oracle/Adoptium Installer: 1. Go to adoptium.net (Eclipse Temurin — the most widely used open-source JDK) 2. Download the Windows .msi installer for Java 21 3. Run the installer — it handles PATH setup automatically 4. Open a new Command Prompt and verify:
Shell
# Verify installation:
java -version
javac -version

# Expected output:
# openjdk version "21.0.1" 2023-10-17
# javac 21.0.1

Installing on macOS

Homebrew is the cleanest way to install and manage Java on macOS. If you don't have Homebrew, install it first from brew.sh.
Shell
# Install Homebrew (if not already installed):
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# Install Java 21 (Eclipse Temurin — open-source JDK):
brew install --cask temurin@21

# Verify:
java -version
javac -version

# If you need to switch between Java versions:
brew install --cask temurin@17   # install another version
/usr/libexec/java_home -V        # list all installed JDKs

Installing on Linux (Ubuntu/Debian)

On Ubuntu and Debian-based systems, OpenJDK is available directly from the package manager — the fastest installation path.
Shell
# Update package index:
sudo apt update

# Install OpenJDK 21:
sudo apt install openjdk-21-jdk

# Verify:
java -version
javac -version

# If multiple Java versions are installed, switch between them:
sudo update-alternatives --config java

# Install on Fedora/RHEL/CentOS:
sudo dnf install java-21-openjdk-devel

# Install on Arch Linux:
sudo pacman -S jdk21-openjdk

Verifying Your Installation

After installation, always verify both commands work — not just java, but also javac. A common issue is having the JRE on PATH but not the JDK tools.
Shell
# Both of these must work:
java -version     # runs the JVM
javac -version    # runs the compiler

# Find where Java is installed:
which java           # shows the path on macOS/Linux
where java           # shows the path on Windows

# On macOS/Linux — find the actual JDK home (not the symlink):
java -XshowSettings:all -version 2>&1 | grep java.home

# Compile and run a test program:
echo 'public class Test { public static void main(String[] a) { System.out.println("Java works!"); } }' > Test.java
javac Test.java
java Test
# Output: Java works!