304

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?

1

14 Answers 14

796

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
Sign up to request clarification or add additional context in comments.

20 Comments

How can I install an older java version?
@TimFulmer I think I am now. I managed to fix whatever problem is was that triggered that 3-day episode of yak shaving. It eventually involved reinstalling brew, ruby, rvm, java, possibly xcode, and every executable file on my computer with an odd number of bytes. Thanks for asking! :)
And don't overlook to create a symlink: 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 :)
Instead of /opt/homebrew I had to do symlink to directory in /usr/local/opt and the way I was able to get the correct location was to run 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.jdk
On my Mac running Big Sur, I had to do: sudo ln -sfn /usr/local/Cellar/openjdk/18.0.2.1/libexec/openjdk.jdk \ /Library/Java/JavaVirtualMachines/openjdk.jdk
|
137

As 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

java -version returns 8 but 11 is installed.
Are you sure the installation of java 11 was successful?
Perhaps you installed java from java.com prior to this? If so follow the instructions found here to remove it. java.com/en/uninstall/uninstall_instructions.html#macOS for macos use: sudo rm -fr /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin sudo rm -fr /Library/PreferencePanes/JavaControlPanel.prefPane sudo rm -fr ~/Library/Application\ Support/Oracle/Java
To symlink it, just use brew link openjdk@11 instead.
The symlink command brew link openjdk@11 Does not work
32

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 too
32

After 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

As a new mac user (switching from Windows) trying to also set this up for the first time, I am not quite clear what you mean by : "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." I assume this is my issue because the alias is not switching to different Java versions like it should and I am not sure how to correct it.
21

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

Great answer. Was only by following your suggestions that I finally got all this to work. Thanks!
You can literally install java and switch with JAVA_VERSION. This is way over engineered.
9

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

Update for recent Homebrew builds: sudo ln -sfn /opt/homebrew/Cellar/openjdk/19.0.2/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
7

If 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

5

If you want to install the Oracle version of java's SDK with homebrew, use the following command:

brew install --cask oracle-jdk

If you don't care, then use the accepted answer by @Tim Fulmer to get the OpenJDK version

brew install java

Comments

3

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"' >> ~/.zshrc
    • echo '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

0
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

Do you mind elaborating? How does this differ from the other answers?
it is exact set of code that is required.
0

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

Please explain how this is different and better wrt all the other answers. I don't see what is added here.
0

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

Read more

Comments

0

Use below commands:

brew install openjdk@21
export PATH="/opt/homebrew/opt/openjdk@21/bin:$PATH"
java --version                                                         
openjdk 21.0.7 2025-04-15
OpenJDK Runtime Environment Homebrew (build 21.0.7)
OpenJDK 64-Bit Server VM Homebrew (build 21.0.7, mixed mode, sharing)

Comments

-1
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

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.