Sunday, October 30, 2022

How to install OpenJDK and Zulu java on macOS

There are several versions of the Java/JDK/JRE software, Oracle, OpenJDK, Azul, etc. All commands are meant to be run as user not root. Test macOS version is 13.0.1 Ventura.

OFFICIAL NATIVE OPENJDK FOR APPLE ARM64

Official OpenJDK binary builds only supports Apple Silicon (Mac/AArch64) starting with version 17 GA (build 17+35), file openjdk-18_macos-aarch64_bin.tar.gz.

1. Check that no Java is installed on your system:

java -version
The operation couldn’t be completed. Unable to locate a Java Runtime.
Please visit http://www.java.com for information on installing Java.

2. Install Homebrew on macOS from Terminal:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

3. Install OpenJDK (current version is 19) from Terminal:

brew reinstall openjdk
==> Downloading https://ghcr.io/v2/homebrew/core/openjdk/manifests/19.0.1
Already downloaded: /Users/user/Library/Caches/Homebrew/downloads/6d230b8f2c89f424f024530bcaf845b2b9df4b396be2af28566e42fc854da043--openjdk-19.0.1.bottle_manifest.json
==> Downloading https://ghcr.io/v2/homebrew/core/openjdk/blobs/sha256:1948f03bd44e133b30b83319a930e653edac721e5550530564def773c7eb0f30
Already downloaded: /Users/user/Library/Caches/Homebrew/downloads/11b30ee4a1f0a23cf8c483f3059e152718e0371ef6d9b11281a5f1c2f3293696--openjdk--19.0.1.arm64_ventura.bottle.tar.gz
==> Reinstalling openjdk 
==> Pouring openjdk--19.0.1.arm64_ventura.bottle.tar.gz
==> 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"

==> Summary
🍺  /opt/homebrew/Cellar/openjdk/19.0.1: 639 files, 320.0MB
==> Running `brew cleanup openjdk`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

4. Get or list all OpenJDK versions installed in your system by brew:

brew ls openjdk | grep 'openjdk.jdk' | head -n 1 | awk -F"openjdk.jdk" '//{print $1"openjdk.jdk"}'            
/opt/homebrew/Cellar/openjdk/19.0.1/libexec/openjdk.jdk

5. Make a symlink from Library to OpenJDK:

sudo ln -sfn /opt/homebrew/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk

6. Now OpenJDK should be installed:

java -version                                                                                                 
openjdk version "19.0.1" 2022-10-18
OpenJDK Runtime Environment Homebrew (build 19.0.1)
OpenJDK 64-Bit Server VM Homebrew (build 19.0.1, mixed mode, sharing)
javac -version
javac 19.0.1

OLDERS JAVA VERSIONS SUPPORTING NATIVELY APPLE ARM64

Since OpenJDK doesn't support earlier versions than Java 17, use third party builds like Azul JDKs (Zulu). You can download desired .DMG files or use brew.sh casks. You can also use native Intel 64-bit builds through Rosetta2 but it's not native to Apple Silicon.

LINKS

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.