avatardatatec.studio

Summary

The article provides a step-by-step guide to uninstall existing JDKs from macOS and install OpenJDK 11 using Homebrew.

Abstract

The article "3 Steps to Install OpenJDK 11 on macOS" outlines a process for macOS users to remove any pre-existing Java Development Kits (JDKs) and install OpenJDK version 11. It begins by instructing users to check for Java installations and remove them if present, either through Homebrew or manually. The second step involves installing Homebrew if it's not already present and then using it to install OpenJDK 11. Finally, the article guides users through creating a symbolic link to the newly installed JDK and verifying the installation by checking the Java version. The author also suggests that there are alternative methods to achieve the same result, such as using an unzipped OpenJDK or Docker. Additionally, the article concludes with recommendations for further Java-related reading and an invitation to follow the author on Medium.

Opinions

  • The author believes that OpenJDK 11 is a preferred JDK version for macOS users, as indicated by the focus on its installation.
  • Homebrew is recommended as a convenient tool for managing Java installations on macOS, as evidenced by its use for both uninstalling existing JDKs and installing OpenJDK 11.
  • The author suggests that there are multiple valid approaches to installing and using Java on macOS, including direct compilation from source or using Docker containers.
  • The article implies that maintaining an up-to-date Java environment is important for Java development, as it provides the latest features and security updates.
  • By providing additional reading material and inviting readers to follow their work, the author expresses a commitment to ongoing education and community engagement in the field of Java development.

3 Steps to Install OpenJDK 11 on macOS

This article will show you how to deinstall all preinstalled JDK from the mac OS and then install OpenJDK 11 on it. I used macOS Big Sur (11.7.2) as example.

TL;DR

sudo rm -fr /Users/yourUserName/Library/Java/JavaVirtualMachines/*
sudo rm -fr /Library/Java/JavaVirtualMachines/*
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install openjdk@11
sudo ln -sfn /usr/local/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk
java -version
  1. Deinstall all JDK
  2. Install OpenJDK 11 via brew
  3. Symlink JDK and check JDK version
  4. What’s Next

1. Deinstall all JDK

1.1 Firstly, execute followed command in Terminal to check if Java is installed on your laptop.

java -version

1.2 No JDK installed. If the output of step 1.1 is something like the text followed, it means that there is no JDK installed in your system. You can then jump to step 2 “Install OpenJDK 11 via brew”.

The operation couldn’t be completed. Unable to locate a Java Runtime.

1.3 Deinstall Java use brew. In case that the output of step 1.1 shows the version of Java and the output contains information about Homebrew (e.g. “OpenJDK Runtime Environment Homebrew”), you can deinstall Java use brew, then go to step 2 “Install OpenJDK 11 via brew”.

# In case the openjdk 8 was found via "java -version"
brew uninstall openjdk@8

1.4 Deinstall Java manuelly

If the output of step 1.1 doesn’t contain Information about Homebrew, please then execute command below.

/usr/libexec/java_home -V

If the output contains information about JavaVirtualMachines, then you need to delete the folder inside JavaVirtualMachines like the example followed.

Output:

Matching Java Virtual Machines (2):

17.0.3 (x86_64) “Amazon.com Inc.” — “Amazon Corretto 17” /Users/yourUserName/Library/Java/JavaVirtualMachines/corretto-17.0.3/Contents/Home

1.8.0_275 (x86_64) “AdoptOpenJDK” — “OpenJDK 8” /Users/yourUserName/Library/Java/JavaVirtualMachines/adopt-openjdk-1.8.0_275/Contents/Home

# Delete the folder inside JavaVirtualMachines

cd /Users/yourUserName/Library/Java/JavaVirtualMachines/

ls

#Remove all subfolder under your JavaVirtualMachines folder.
sudo rm -fr /Users/yourUserName/Library/Java/JavaVirtualMachines/adopt-openjdk-1.8.0_275 
sudo rm -fr /Users/yourUserName/Library/Java/JavaVirtualMachines/corretto-17.0.3

1.5 Further Option

If output of step 1.4 doesn’t contains Information about JavaVirtualMachines, you might also need to check if there are folder inside path /Library/Java/JavaVirtualMachines. In case there are any folder there, please use command below to remove them.

sudo rm -fr /Library/Java/JavaVirtualMachines/*

1.6 Verify jdk is removed.

Run this command and none jdk should be found.

java -version

The output should be something 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.

2. Install OpenJDK11 via brew

2.1 Install brew on mac OS. You can find details on their official website.

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

2.2 Install OpenJDK 11

This step might take a few minutes.

brew install openjdk@11

3. Symlink JDK and check JDK version

3.1 Check the related info of installed openjdk 11.

brew search java

The output might be following:

==> Formulae

app-engine-java java-service-wrapper javarepl pdftk-java

google-java-format java11 ✔ jslint4java

java javacc libreadline-java

3.2 Show related information of installed java 11

brew info java11

The the Output might be:

……

For the system Java wrappers to find this JDK, symlink it with

sudo ln -sfn /usr/local/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk

openjdk@11 is keg-only, which means it was not symlinked into /usr/local,

because this is an alternate version of another formula.

If you need to have openjdk@11 first in your PATH, run:

echo ‘export PATH=”/usr/local/opt/openjdk@11/bin:$PATH”’ >> ~/.zshrc

……

3.3 Symlink the OpenJDK11

Use the information from step 3.2 and execute the command suggested before:

sudo ln -sfn /usr/local/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk

3.4 Check Java version

java -version

The output should be like this:

openjdk version “11.0.17” 2022–10–18

OpenJDK Runtime Environment Homebrew (build 11.0.17+0)

OpenJDK 64-Bit Server VM Homebrew (build 11.0.17+0, mixed mode)

Great! It’s done.

Please also keep in mind, there are also different ways to make it work, like use unzipped OpenJDK, directly compile the source or use docker etc.

4. What’s Next

Following posts from mine are also related to Java, these might be the next jounery for you:

More articles can be found here:

I hope you enjoyed today’s content.

You are welcome to my network:

Follow me on Medium

Your claps 👏 keep me continue writing high-quality articles. Thank you!

Openjdk11
Brew
Macos
Java
Symlink
Recommended from ReadMedium