install java on mac_How to install Java on Mac OSX

夹谷茂
2023-12-01

This article shows you how to install Java on Mac OSX, and also how to do Java/JDK version switching.

Tested with

Mac OS 10.15.3

JDK 8, 9, 10, 11, 12, 13 (AdoptOpenJDK)

Note

On Mac OSX, Java should installed in this folder; it is Mac’s standard.

/Library/Java/JavaVirtualMachines/

We will show you two ways to install Java JDK on Mac, via the popular Homebrew package manager and manually installation.

1. Homebrew

1.1 Install Homebrew and update it.

$ brew update

$ brew tap adoptopenjdk/openjdk

1.3 Find all available JDK.

$ brew search jdk

The output may vary.

==> Casks

adoptopenjdk adoptopenjdk12 adoptopenjdk13-openj9 adoptopenjdk8-openj9-jre

adoptopenjdk10 adoptopenjdk12-jre adoptopenjdk13-openj9-jre adoptopenjdk8-openj9-jre-large

adoptopenjdk11 adoptopenjdk12-openj9 adoptopenjdk13-openj9-jre-large adoptopenjdk8-openj9-large

adoptopenjdk11-jre adoptopenjdk12-openj9-jre adoptopenjdk13-openj9-large adoptopenjdk9

adoptopenjdk11-openj9 adoptopenjdk12-openj9-jre-large adoptopenjdk8 oracle-jdk

adoptopenjdk11-openj9-jre adoptopenjdk12-openj9-large adoptopenjdk8 oracle-jdk-javadoc

adoptopenjdk11-openj9-jre-large adoptopenjdk13 adoptopenjdk8-jre sapmachine-jdk

adoptopenjdk11-openj9-large adoptopenjdk13-jre adoptopenjdk8-openj9

1.4 Java 8, 9, 10, 11, 12, 13, choose one to install.

brew cask install adoptopenjdk8

brew cask install adoptopenjdk9

brew cask install adoptopenjdk10

brew cask install adoptopenjdk11

brew cask install adoptopenjdk12

brew cask install adoptopenjdk13

This example will install Java 8 and 11 for testing.

$ brew cask install adoptopenjdk8

$ brew cask install adoptopenjdk11

1.5 Where Java is installed? /usr/libexec/java_home -V

$ /usr/libexec/java_home -V

Matching Java Virtual Machines (2):

11.0.6, x86_64:"AdoptOpenJDK 11"/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home

1.8.0_242, x86_64:"AdoptOpenJDK 8"/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

Homebrew will install the JDK at folder /Library/Java/JavaVirtualMachines/, and this folder is Mac’s standard folder for Java installs.

1.6 Test it, by default, Mac takes the highest version.

$ java -version

openjdk version "11.0.6" 2020-01-14

OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.6+10)

OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.6+10, mixed mode)

1.7 How to do version switching? Refer to below 3. version switching

2. Manual installation

The example shows you how to download the early access JDK 14 and install it on Mac OSX.

Steps

Download JDK.

Puts the JDK folder at /Library/Java/JavaVirtualMachines.

Export JAVA_HOME.

2.1 Download the early access JDK 14 from the OpenJDK website.

2.2 Extracts tar file to /Library/Java/JavaVirtualMachines

$ cd /Library/Java/JavaVirtualMachines

$ sudo cp ~/Downloads/openjdk-14_osx-x64_bin.tar.gz /Library/Java/JavaVirtualMachines

$ pwd

/Library/Java/JavaVirtualMachines

$ sudo tar xzf openjdk-14_osx-x64_bin.tar.gz

$ sudo rm openjdk-14_osx-x64_bin.tar.gz

2.3 Export JAVA_HOME.

Find out where is JDK 14.

$ /usr/libexec/java_home -v14

/Library/Java/JavaVirtualMachines/jdk-14.jdk/Contents/Home

Create or edit the existing ~/.bash_profile with a text editor.

$ vim ~/.bash_profile

Export JAVA_HOME, save and exit.

~/.bash_profile

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-14.jdk/Contents/Home

Reflect the changes

$ source ~/.bash_profile

2.4 Test it.

$ java -version

openjdk version "14" 2020-03-17

OpenJDK Runtime Environment (build 14+36-1461)

OpenJDK 64-Bit Server VM (build 14+36-1461, mixed mode, sharing)

$ echo $JAVA_HOME

/Library/Java/JavaVirtualMachines/jdk-14.jdk/Contents/Home

3. Version Switching

In this tutorial, we installed the Java 8 and Java 11 via Homebrew (Step 1), and Java 14 manually (Step 2), so this Mac has three versions now. And the Mac is using the JDK 14.

$ /usr/libexec/java_home -V

Matching Java Virtual Machines (3):

14, x86_64:"OpenJDK 14"/Library/Java/JavaVirtualMachines/jdk-14.jdk/Contents/Home

11.0.6, x86_64:"AdoptOpenJDK 11"/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home

1.8.0_242, x86_64:"AdoptOpenJDK 8"/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk-14.jdk/Contents/Home

Note

There are tools like jEnv to manage the Java version switching, but I prefer to manage with export JAVA_HOME manually, it’s simple and easy to understand, no black box magic.

3.1 Create or edit the existing ~/.bash_profile with a text editor like vim or nano, export JAVA_HOME to the specified JDK we want to use on Mac.

$ vim ~/.bash_profile

~/.bash_profile

export JAVA_HOME_8=$(/usr/libexec/java_home -v1.8)

export JAVA_HOME_11=$(/usr/libexec/java_home -v11)

export JAVA_HOME_14=$(/usr/libexec/java_home -v14)

# Java 8

export JAVA_HOME=$JAVA_HOME_8

# Java 11

# export JAVA_HOME=$JAVA_HOME_11

The above changes will make JAVA_HOME point to JAVA 8.

3.2 Reflect the changes.

$ source ~/.bash_profile

$ java -version

openjdk version "1.8.0_242"

OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_242-b08)

OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.242-b08, mixed mode)

Done.

If we want to switch JDK version again, update the ~/.bash_profile and export JAVA_HOME to other JDK version.

References

 类似资料:

相关阅读

相关文章

相关问答