Skip to content

Latest commit

 

History

History
107 lines (76 loc) · 3.17 KB

java_version_manage_osx.md

File metadata and controls

107 lines (76 loc) · 3.17 KB

Multiple Java Version on Mac OSX

Since MacOSX 10.5, Apple deliver /usr/libexec/java_home to manage JAVA_HOME

Which java version you have ?

$ /usr/libexec/java_home  -V
Matching Java Virtual Machines (3):
    1.8.291.10 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
    1.8.0_231 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home
    1.7.0_292 (x86_64) "UNDEFINED" - "Zulu 7.44.0.11" /Library/Java/JavaVirtualMachines/zulu-7.jdk/Contents/Home

Install another JDK@11 via homebrew

$ brew install openjdk@11
# soft link to /Library/Java/JavaVirtualMachines
# so that `java_home` cmd can detect it
$ sudo ln -sfn /usr/local/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk

now jdk11 are available

$ /usr/libexec/java_home  -V
Matching Java Virtual Machines (4):
    11.0.16.1 (x86_64) "Homebrew" - "OpenJDK 11.0.16.1" /usr/local/Cellar/openjdk@11/11.0.16.1/libexec/openjdk.jdk/Contents/Home
    1.8.291.10 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
    1.8.0_231 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home
    1.7.0_292 (x86_64) "UNDEFINED" - "Zulu 7.44.0.11" /Library/Java/JavaVirtualMachines/zulu-7.jdk/Contents/Home

Get JAVA_HOME via java_home

$ /usr/libexec/java_home -v11   
/usr/local/Cellar/openjdk@11/11.0.16.1/libexec/openjdk.jdk/Contents/Home
$ /usr/libexec/java_home -v1.8.0
/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home
$ /usr/libexec/java_home -v1.7  
/Library/Java/JavaVirtualMachines/zulu-7.jdk/Contents/Home

Manage Your Jave Environment

# ~/.profile
export JAVA_7_HOME=$(/usr/libexec/java_home -v1.7)
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8.0)
export JAVA_11_HOME=$(/usr/libexec/java_home -v11)

alias java7='export JAVA_HOME=$JAVA_7_HOME'
alias java8='export JAVA_HOME=$JAVA_8_HOME'
alias java11='export JAVA_HOME=$JAVA_11_HOME'

#default java11
export JAVA_HOME=$JAVA_11_HOME

switch java version

$ java8
$ java -version
openjdk version "1.8.0_312"
$ java11
$ java -version
openjdk version "11.0.12" 2021-07-20

if only you need java7...

# 1 install java7
brew tap homebrew/cask-versions
brew cask install homebrew/cask-versions/zulu7