154

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?

Tim Fulmer
  • 14,970
  • 7
  • 28
  • 34
  • If you want openjdk: `brew install openjdk` [https://formulae.brew.sh/formula/openjdk](https://formulae.brew.sh/formula/openjdk) – cellepo Feb 12 '23 at 03:34

8 Answers8

396

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
Jacob Ford
  • 4,553
  • 5
  • 27
  • 42
Tim Fulmer
  • 14,970
  • 7
  • 28
  • 34
  • 4
    How can I install an older java version? – kikulikov Jan 21 '21 at 10:54
  • Oh, this is a good question @kikulikov, maybe try this, ymmv: https://github.com/AdoptOpenJDK/homebrew-openjdk – Tim Fulmer Jan 26 '21 at 01:34
  • 1
    when I do `brew install java` I get `Error: No available formula or cask with the name "java". It was migrated from homebrew/cask to homebrew/core.` (preceded by `These similarly named formulae were found: app-engine-java...`) – Sigfried Mar 01 '21 at 11:24
  • @Sigfried are you on the latest version of brew? – Tim Fulmer Mar 05 '21 at 00:47
  • 2
    @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! :) – Sigfried Mar 05 '21 at 12:30
  • Haha @Sigfried! Yes, the wipe everything approach. Been there, done that plenty. Personally, I use `brew` for system packages (sometimes java), `nvm` for node, and for python please please be very careful to use proper virtual environments. Modifying the system python on any distro has never gone well for me. – Tim Fulmer Mar 10 '21 at 18:15
  • 3
    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` :) – bathyscapher Jan 20 '22 at 12:27
  • Thanks for the symlink comment @bathyscapher. This was missing from the question and is an essential step (I had installed openjdk but still got an error). I've edited the question to add the symlink. – Cornelius Roemer Aug 12 '22 at 19:44
  • 29
    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` – Elijah Lofgren Aug 18 '22 at 15:59
  • 3
    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 – bruceg Sep 22 '22 at 00:01
  • I had to `source ~/.zshrc` after symlinking – jtschoonhoven Dec 02 '22 at 00:34
  • 1
    You can easily find the symlink by running `brew info openjdk@your-java-version` and your symlink will be specified in the response – lionHeart Jan 12 '23 at 08:39
  • If you are skeptical of the idea of need for `sudo ln`, the command is given after the installation of the package from `brew`. You can see this recommendation by running `brew info java`/`brew info openjdk`. – mkobit Jan 12 '23 at 16:22
  • For the symlink I had to do following: sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk. Double check with "brew info java" which symlink you have to trigger – Christian X Feb 02 '23 at 09:01
  • @ElijahLofgren info of `brew info openjdk` provided me the same solution as the post. Thanks! – Darky WC Mar 07 '23 at 09:55
80

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
Cornelius Roemer
  • 3,772
  • 1
  • 24
  • 55
bathyscapher
  • 1,615
  • 1
  • 13
  • 18
  • java -version returns 8 but 11 is installed. – DragonKnight Jul 11 '22 at 07:22
  • Are you sure the installation of java 11 was successful? – bathyscapher Jul 14 '22 at 07:05
  • 1
    Perhaps you installed java from java.com prior to this? If so follow the instructions found here to remove it. https://www.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` – codeskin Sep 15 '22 at 15:17
  • To symlink it, just use `brew link openjdk@11` instead. – jackblk Dec 13 '22 at 09:05
15

I had to sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk

Mark Simon
  • 612
  • 1
  • 6
  • 17
5

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 speciifed 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...
lionHeart
  • 507
  • 5
  • 14
5

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.

John Tran
  • 461
  • 5
  • 5
4

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.

Gerard
  • 95
  • 1
  • 10
  • Update for recent Homebrew builds: `sudo ln -sfn /opt/homebrew/Cellar/openjdk/19.0.2/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk` – Andrew Bowman Feb 28 '23 at 16:57
3

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

ThinkBonobo
  • 15,487
  • 9
  • 65
  • 80
-2
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
Akshay Vijay Jain
  • 13,461
  • 8
  • 60
  • 73