Skip to content

Install OpenJDK

Zhamri Che Ani edited this page Nov 19, 2025 · 11 revisions

Install OpenJDK 21 using Homebrew

1. Update Homebrew

brew update

2. Install OpenJDK 21

brew install openjdk@21

Output:

For the system Java wrappers to find this JDK, symlink it with
  sudo ln -sfn /usr/local/opt/openjdk@21/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-21.jdk

openjdk@21 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have openjdk@21 first in your PATH, run:
  echo 'export PATH="/usr/local/opt/openjdk@21/bin:$PATH"' >> ~/.zshrc

For compilers to find openjdk@21 you may need to set:
  export CPPFLAGS="-I/usr/local/opt/openjdk@21/include"
(base) ~ % 

3. Link OpenJDK 21 to your system

macOS does not automatically link newer JDKs, so you must link it manually:

sudo ln -sfn /usr/local/opt/openjdk@21/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-21.jdk

(Use /opt/homebrew instead of /usr/local if you're on Apple Silicon.)

4. Check installed Java versions on Mac

/usr/libexec/java_home -V

Export path

If you need to have this software first in your PATH instead consider running:

echo 'export PATH="/usr/local/opt/openjdk@21/bin:$PATH"' >> ~/.zshrc

Set JAVA_HOME

Add this to your shell config.

For zsh (~/.zshrc):

echo 'export JAVA_HOME="$(/usr/libexec/java_home -v 21)"' >> ~/.zshrc
source ~/.zshrc

For bash (~/.bash_profile):

echo 'export JAVA_HOME="$(/usr/libexec/java_home -v 21)"' >> ~/.bash_profile
source ~/.bash_profile

Verify

java -version

Expected output:

openjdk version "21.0.9" 2025-10-21
OpenJDK Runtime Environment Homebrew (build 21.0.9)
OpenJDK 64-Bit Server VM Homebrew (build 21.0.9, mixed mode, sharing)

Check JAVA_HOME

echo $JAVA_HOME

Clone this wiki locally