This post goes over how to use the “java” command-line tool on macOS.
Apple’s Java is a legacy version:
which java # /usr/bin/java
When you try to use
java in the CLI:
You will get the pop-up:
To use the "java" command-line tool you need to install a JDK.
brew install java
Once it’s installed, look at
==> 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
~/.zshrcwith your shell configuration file.
Reload your shell profile:
java binary and version:
which java # /usr/local/opt/openjdk/bin/java