I'd like to setup java on a new OS X machine, and prefer to use brew for OS X package management. How can I install latest java using brew?
14 Answers
Turns out java has been moved into brew core recently, so the correct command as of August 2022 is:
brew install java
Then check your installation by running
java -version
If the result does not looks like this:
openjdk 18.0.2 2022-07-19
OpenJDK Runtime Environment Homebrew (build 18.0.2+0)
OpenJDK 64-Bit Server VM Homebrew (build 18.0.2+0, mixed mode, sharing)
but like this:
The operation couldn’t be completed. Unable to locate a Java Runtime.
Please visit http://www.java.com for information on installing Java.
Then you also need to create a symlink for the system Java wrappers to find this JDK:
sudo ln -sfn /opt/homebrew/opt/openjdk/libexec/openjdk.jdk \
/Library/Java/JavaVirtualMachines/openjdk.jdk
20 Comments
For the system Java wrappers to find this JDK, symlink it with sudo ln -sfn /opt/homebrew/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk :)brew info openjdk and run the command after 'For the system Java wrappers to find this JDK, symlink it with' which in my case was sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdkAs an add-on to the accepted answer: to install a certain version of Java, e.g. version 11, run:
brew install openjdk@11
And symlink it:
sudo ln -sfn /opt/homebrew/opt/openjdk@11/libexec/openjdk.jdk \
/Library/Java/JavaVirtualMachines/openjdk-11.jdk
5 Comments
sudo rm -fr /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin sudo rm -fr /Library/PreferencePanes/JavaControlPanel.prefPane sudo rm -fr ~/Library/Application\ Support/Oracle/Javabrew link openjdk@11 instead.I had to sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
1 Comment
brew info java shows the current command to run, this was the same for me tooAfter multiple tries, here is a recipe that allows for me to switch between the different versions of OpenJDK and this approach appears to work on Ventura.
First, install all the necessary OpenJDK versions (for me I needed 8, 11, 17, and 20)
brew install openjdk@8
brew install openjdk@11
brew install openjdk@17
brew install openjdk@20
Update the wrapper, as recommended in the multiple postings: sudo rm -f /Library/Java/JavaVirtualMachines/openjdk.jdk and sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk.
Update your .zshrc as follows
alias j8="export JAVA_HOME=/usr/local/opt/openjdk@8 ; java -version"
alias j11="export JAVA_HOME=/usr/local/opt/openjdk@11 ; java -version"
alias j17="export JAVA_HOME=/usr/local/opt/openjdk@17 ; java -version"
alias j20="export JAVA_HOME=/usr/local/opt/openjdk@20 ; java -version"
To switch your version, run the alias j8, j11, etc.
Don't forget to prepend the $PATH in your ~/.zshrc with export PATH="/usr/local/bin:/usr/local/sbin:$PATH" otherwise the system java will have precedent.
Note that this approach is particularly useful because I rely on brew as my primary (and arguably most consistent) package manager.
1 Comment
Assembled from the answers here and How to set or change the default Java (JDK) version on macOS?:
You can use brew to install multiple Java versions and run a command to switch between the versions as required.
Example
Install two Java versions (change Java versions as pleased):
brew install openjdk@19
brew install openjdk@8
Use the following command to see the installed versions:
/usr/libexec/java_home -V
you should see the two versions specified in the response (if not, read further to create a symlink). Now you can select the Java version using:
export JAVA_HOME=`/usr/libexec/java_home -v 8`
verify selected version:
java -version
you can further export the JAVA_HOME variable in your shell init file as specified in the attached SOF thread.
Now, in case you do not see the Java version in /usr/libexec/java_home as expected and the version selection of that missing version is not working, you might need to add a symlink:
Executing brew info openjdk@the-missing-java-versionshould return the location of the installed version and will specify a symlink command that you should run for the system to find the SDK. The response text look something similar to:
...For the system Java wrappers to find this JDK, symlink it with
sudo ln -sfn /opt/homebrew/opt/openjdk@17/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-17.jdk...
2 Comments
run
brew reinstall openjdk
and it will display the following which shows your file path:
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"' >> /Users/gerarddonnelly/.bash_profile
For compilers to find openjdk you may need to set:
export CPPFLAGS="-I/usr/local/opt/openjdk/include"
I then ran the code below, which I took from the output above:
sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
The other issue I had was that Elasticsearch was not recognising my JAVA version. To fix that I added the following line to my .bash_profile
EXPORT JAVA_HOME=$(/usr/libexec/java_home)
then I ran
source ~/.bash_profile
to refresh my profile.
After that it worked.
Hope this helps.
1 Comment
sudo ln -sfn /opt/homebrew/Cellar/openjdk/19.0.2/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdkIf symlink doesn't work
I was having some issues getting this to work, but found a solution so thought I'd post.
If, after running brew install java or brew install openjdk, you get
The operation couldn’t be completed. Unable to locate a Java Runtime.
Please visit http://www.java.com for information on installing Java.
and the previously listed symlinks do not work, you can find out information about the install by running brew info <package>. The output will include the necessary symlink command, which may be different than previously posted. After running that command, I am up and running.
Comments
Use jenv + Homebrew on macOS:
brew install jenv- Configure Zsh (use other instructions here if applicable https://www.jenv.be)
echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.zshrcecho 'eval "$(jenv init -)"' >> ~/.zshrc
eval "$(jenv init -)"(initialize jenv)brew install openjdk@23(or the version you want)jenv add /opt/homebrew/opt/openjdk@23/libexec/openjdk.jdk/Contents/Home/jenv global openjdk64-23.0.1(set global java version)- restart shell
Comments
brew tap homebrew/cask-versions
brew update
brew install openjdk@19
sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
echo 'export PATH="/usr/local/opt/openjdk/bin:$PATH"' >> ~/.zshrc
2 Comments
If anyone is stuck on Mac OS Monterey, installing Java 11 using the brew command is what worked for me. It might be similar for previous version (not too sure though).
brew tap AdoptOpenJDK/openjdk
brew cask install adoptopenjdk11
1 Comment
For Mac:
Go to finder and find Android studio:
- right click and click show package contents
- Create a new folder called jre
- copy the contents of the jbr folder and paste them into jre folder
- reload your terminal and type flutter doctor
Comments
acaibird: ~/Library/Java/JavaVirtualMachines brew info java
==> openjdk: stable 21.0.2 (bottled) [keg-only]
Development kit for the Java programming language
https://openjdk.java.net/
/opt/homebrew/Cellar/openjdk/21.0.2 (600 files, 331.2MB)
Poured from bottle using the formulae.brew.sh API on 2024-01-28 at 21:25:54
From: https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git/Formula/o/openjdk.rb
License: GPL-2.0-only with Classpath-exception-2.0
==> Dependencies
Build: autoconf ✔, pkg-config ✔
Required: giflib ✔, harfbuzz ✔, jpeg-turbo ✔, libpng ✔, little-cms2 ✔
==> Requirements
Build: Xcode (on macOS) ✔
Required: macOS >= 10.15 (or Linux) ✔
==> Caveats
For the system Java wrappers to find this JDK, symlink it with
sudo ln -sfn /opt/homebrew/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
openjdk is keg-only, which means it was not symlinked into /opt/homebrew,
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="/opt/homebrew/opt/openjdk/bin:$PATH"' >> ~/.zshrc
For compilers to find openjdk you may need to set:
export CPPFLAGS="-I/opt/homebrew/opt/openjdk/include"
==> Analytics
install: 87,975 (30 days), 237,751 (90 days), 869,053 (365 days)
install-on-request: 48,996 (30 days), 131,628 (90 days), 506,308 (365 days)
build-error: 394 (30 days)
sudo ln -sfn /opt/homebrew/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
brew install openjdkhttps://formulae.brew.sh/formula/openjdk