18

I'm trying to install JDK 17 to macOS BigSur (11.5.2) using brew install openjdk@17

> brew install openjdk@17
Running `brew update --preinstall`...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
Updated 3 formulae.

openjdk  is already installed but outdated (so it will be upgraded).
==> Downloading https://ghcr.io/v2/homebrew/core/openjdk/manifests/17.0.1_1
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/openjdk/blobs/sha256:75ee17c1331022fa8bf1e63f00fe903f23fe31d3a09021117d46b5f6ed1e26e1
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:75ee17c1331022fa8bf1e63f00fe903f23fe31d3a09021117d46b5f6ed1e26e1?se=2021-12-09T20%3A25%3A00Z&sig=NnB%2FO%2BGQF5ec6iqdKA4w29
######################################################################## 100.0%
==> Pouring openjdk--17.0.1_1.big_sur.bottle.tar.gz
==> 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"

After installation jenv can't find the instance of java17

jenv versions                                                                                                                               
  system
  1.8
  1.8.0.161
  1.8.0.181
* 11.0
  11.0.1
  15.0
  15.0.1
  openjdk64-11.0.1
  openjdk64-15.0.1
  oracle64-1.8.0.161
  oracle64-1.8.0.181
lazylead
  • 1,453
  • 1
  • 14
  • 26

1 Answers1

38

Found that you need to register the java-17 dist in jenv using command:

jenv add /usr/local/opt/openjdk@17/libexec/openjdk.jdk/Contents/Home 

The path to /usr/local/opt/openjdk@17/libexec/openjdk.jdk you may take from your brew installation logs (found on the line sudo ln -sfn /usr/local/opt/openjdk@17/libexec/openjdk.jdk ...) and just add /Contents/Home

Now, it works

> jenv local 17.0
> java -version                                                                                                                                     
openjdk version "17.0.1" 2021-10-19
OpenJDK Runtime Environment Homebrew (build 17.0.1+1)
OpenJDK 64-Bit Server VM Homebrew (build 17.0.1+1, mixed mode, sharing)
lazylead
  • 1,453
  • 1
  • 14
  • 26
  • 3
    just wanted to add that as of 05/04/2022, with the latest jdk being 18, the above command will add jdk18 to your jenv. If you specifically need jdk17 - do this: jenv add /usr/local/opt/openjdk@17/libexec/openjdk.jdk/Contents/Home – Marina May 04 '22 at 17:06
  • 2
    Great thanks. On my Mac it was /opt/homebrew/Cellar/openjdk@11/11.0.16/libexec/openjdk.jdk/Contents/Home for brew isntall java11 – Robert Alexander Aug 02 '22 at 12:28
  • 2
    @RobertAlexander I don't recommend linking to the cellar - otherwise it'll break when you upgrade to a new version. – childofsoong Dec 08 '22 at 20:05
  • I see and what is the best/more correct way of handling this? – Robert Alexander Dec 09 '22 at 16:06
  • 4
    @RobertAlexander in your case (apple silicon) for homebrew installations, the "correct" way would be to `jenv add /opt/homebrew/opt/openjdk@11` (see https://github.com/Homebrew/discussions/discussions/239#discussioncomment-162802 and https://docs.brew.sh/FAQ#why-should-i-install-homebrew-in-the-default-location) – user25959 Feb 05 '23 at 21:19
  • Very informative, thanks a lot. – Robert Alexander Feb 06 '23 at 09:05