This post goes over how to use the “java” command-line tool on macOS.
Problem
Apple’s Java is a legacy version:
which java # /usr/bin/java
When you try to use java
in the CLI:
java -version
You will get the pop-up:
To use the "java" command-line tool you need to install a JDK.
Solution
brew install java
Once it’s installed, look at Caveats
:
==> Caveats
For the system Java wrappers to find this JDK, symlink it with
sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
openjdk is keg-only, which means it was not symlinked into /usr/local,
because macOS provides similar software and installing this software in
parallel can cause all kinds of trouble.
If you need to have openjdk first in your PATH, run:
echo 'export PATH="/usr/local/opt/openjdk/bin:$PATH"' >> ~/.zshrc
For compilers to find openjdk you may need to set:
export CPPFLAGS="-I/usr/local/opt/openjdk/include"
You can either symlink the JDK with:
sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
Or export the openjdk first in your PATH:
echo 'export PATH="/usr/local/opt/openjdk/bin:$PATH"' >> ~/.zshrc
If you’re not using ZSH, make sure to replace
~/.zshrc
with your shell configuration file.
Reload your shell profile:
source ~/.zshrc
Check the java
binary and version:
which java # /usr/local/opt/openjdk/bin/java
java -version