-
Notifications
You must be signed in to change notification settings - Fork 0
Install OpenJDK
Zhamri Che Ani edited this page Nov 21, 2025
·
11 revisions
brew update
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) ~ %
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
OR
sudo ln -sfn /opt/homebrew/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.)
/usr/libexec/java_home -V
Output (Apple Silicon)
Matching Java Virtual Machines (1):
21.0.9 (arm64) "Homebrew" - "OpenJDK 21.0.9" /opt/homebrew/Cellar/openjdk@21/21.0.9/libexec/openjdk.jdk/Contents/Home
/opt/homebrew/Cellar/openjdk@21/21.0.9/libexec/openjdk.jdk/Contents/Home
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
OR
echo 'export PATH="/opt/homebrew/opt/openjdk@21/bin:$PATH"' >> ~/.zshrc (Apple Silicon)
Add this to your shell config.
echo 'export JAVA_HOME="$(/usr/libexec/java_home -v 21)"' >> ~/.zshrc
source ~/.zshrc
echo 'export JAVA_HOME="$(/usr/libexec/java_home -v 21)"' >> ~/.bash_profile
source ~/.bash_profile
Add this to ~/.zprofile:
export JAVA_HOME="/opt/homebrew/opt/openjdk@21/libexec/openjdk.jdk/Contents/Home"
export PATH="$JAVA_HOME/bin:$PATH"
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)
echo $JAVA_HOME
Output:
/usr/local/Cellar/openjdk@21/21.0.9/libexec/openjdk.jdk/Contents/Home