Skip to content
Commitpedia
Log in Join & edit

Install Java (OpenJDK) on Linux, macOS and Windows

Install a current long-term-support OpenJDK with the compiler included, make java and javac available on the command line, and switch between versions when projects need different ones.

Drafted with AI from official documentation. Scripts were run in a test environment and repository links verified. Awaiting a human check. Did it work for you? Last edited Outdated?

Edit history of Install Java (OpenJDK) on Linux, macOS and Windows: 1 recorded edit
Edit history · 1 edit
Type
Article
Edits
1
Editors
1
First issued
15 Oct 2026
Last edit
15 Oct 2026
Reading
2 min

Which option to pick

Java is specified by Oracle but built by many vendors from the same OpenJDK source: Eclipse Temurin, Microsoft Build of OpenJDK, Amazon Corretto, the distribution’s own packages, and Oracle’s. For development they are interchangeable; pick what your package manager offers. Install a JDK (development kit, includes javac), not a JRE. Choose the newest LTS version (21 at the time of writing, with 25 as the next LTS) unless the project you work on pins another. If you need to juggle several versions, SDKMAN! on Linux and macOS makes switching easy.

Step: Linux

Debian and Ubuntu (default-jdk picks the distribution’s current OpenJDK; use a numbered package to choose):

Check before you run this

  • Runs with administrator rights. Make sure you understand every line.
sudo apt update
sudo apt install default-jdk
sudo apt install openjdk-21-jdk

Fedora and RHEL:

Check before you run this

  • Runs with administrator rights. Make sure you understand every line.
sudo dnf install java-21-openjdk-devel

Or with SDKMAN!, which works on any distribution and manages versions in your home directory:

Check before you run this

  • Downloads a file and runs it straight away. Open the URL and read the script first.
curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
sdk list java
sdk install java 21-tem

Step: macOS

Check before you run this

  • Runs with administrator rights. Make sure you understand every line.
brew install openjdk@21
sudo ln -sfn "$(brew --prefix)/opt/openjdk@21/libexec/openjdk.jdk" /Library/Java/JavaVirtualMachines/openjdk-21.jdk

The symlink is the step Homebrew prints in its caveats; it registers the JDK with macOS so java and /usr/libexec/java_home find it. Alternatively brew install --cask temurin@21 installs Eclipse Temurin as a normal macOS package, or SDKMAN! works exactly as on Linux.

Step: Windows

winget install Microsoft.OpenJDK.21

Or winget install EclipseAdoptium.Temurin.21.JDK, or download the .msi installer from adoptium.net. The installers set JAVA_HOME and add the bin directory to PATH; open a new terminal afterwards.

Check it works

java -version
javac -version

Both should report the same version, for example openjdk 21.0.x. Note that java uses a single dash for -version. A first compile and run:

cat > Hello.java <<'EOF'
public class Hello {
    public static void main(String[] args) {
        System.out.println("hello from java");
    }
}
EOF
java Hello.java

Common problems

  • javac: command not found while java works: only a JRE is installed. Install the -jdk or -devel package.
  • The wrong version runs when several are installed: on Debian and Ubuntu use sudo update-alternatives --config java (and the same for javac); on macOS set export JAVA_HOME=$(/usr/libexec/java_home -v 21); with SDKMAN! run sdk use java 21-tem.
  • Build tools such as Maven or Gradle complain about JAVA_HOME: set it to the JDK directory, for example /usr/lib/jvm/java-21-openjdk-amd64 on Ubuntu, in your shell startup file or Windows environment variables.
  • On macOS, java shows a dialog saying no Java runtime is present even after brew install: the symlink step above was skipped.

Official documentation

Did this work for you?

No votes yet. Be the first.

If it did not work, the report form below tells the editors what went wrong.

See also

  • 0386 Install Node.js on Linux, macOS and Windows Get Node.js and npm on any desktop OS. Use a version manager (nvm or fnm) when you work on several projects, or… Beginner · Step by step · Linux, macOS, Windows Article Installing languages & tools · Node.js 15 Oct 2026
  • 0387 Install Python on Linux, macOS and Windows Install a current Python 3 with pip and venv on each desktop OS, check the version, and avoid the classic traps: python… Beginner · Step by step · Linux, macOS, Windows Article Environments & packages · Installing languages & tools 15 Oct 2026
  • 0388 Install Go on Linux, macOS and Windows Install the Go toolchain from the official downloads or a package manager, set PATH so the go command and installed tools are… Beginner · Step by step · Linux, macOS, Windows Article Installing languages & tools 15 Oct 2026
  • 0390 Install Git on Linux, macOS and Windows Install Git from your package manager, Xcode's command line tools or the Windows installer, confirm the version, and do the one-time configuration… Beginner · Step by step · Linux, macOS, Windows Article Installing languages & tools · Setup 15 Oct 2026
  • 0391 Install Docker on Linux, macOS and Windows Docker Desktop or Docker Engine? Pick the right one for your OS, install it, and confirm with docker run hello-world. Covers the… Beginner · Step by step · Linux, macOS, Windows Article Docker basics · Installing languages & tools 15 Oct 2026
  • 0392 Install Visual Studio Code on Linux, macOS and Windows Install VS Code from Microsoft's packages, Homebrew or winget, get the code command working in the terminal, and add the extensions you… Beginner · Step by step · Linux, macOS, Windows Article Command-line tools · Installing languages & tools 15 Oct 2026

History

Every saved edit is kept. The plot on the sleeve draws one line per edit. Open any edit to see exactly what changed.

  1. Dominik +2,981 chars

Report a problem

Something wrong, unsafe or copied? Tell the editors.
What is wrong?