简体   繁体   中英

change java version in macOS BigSur

I can't change the java version in macOS BigSur. i have the following versions:

when I execute this command

/usr/libexec/java_home -V

i have the following versions:

Matching Java Virtual Machines (3):
    11.0.8 (x86_64) "Oracle Corporation" - "Java SE 11.0.8" /Library/Java/JavaVirtualMachines/jdk-11.0.8.jdk/Contents/Home
    1.8.261.12 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
    1.8.0_261 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk-11.0.8.jdk/Contents/Home

i want to switch to java 1.8 as the default one, so I execute this command:

export JAVA_HOME=/usr/libexec/java_home -v 1.8.0_261

and then when I check java version:

java -version

I still got the old (higher one)

java version "11.0.8" 2020-07-14 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.8+10-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.8+10-LTS, mixed mode)

why it's not working?

请使用以下命令:-

export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_261`

you have to unset JAVA_HOME and set JAVA_VERSION then it works.

~% JAVA_VERSION=12 java -version
openjdk version "14.0.2" 2020-07-14
~% export JAVA_VERSION=12
~% java -version
openjdk version "14.0.2" 2020-07-14
~% unset JAVA_HOME
~% java -version
java version "12.0.1" 2019-04-16
~% JAVA_VERSION=13 java -version
java version "13-ea" 2019-09-17

or if you need to set JAVA_HOME you need to unset before calling /usr/libexec/java_home

function jav {
unset JAVA_HOME
export JAVA_HOME=`/usr/libexec/java_home -v $@`
}
~% jav 12
~% java -version
java version "12.0.1" 2019-04-16
~% jav 13
~% java -version
java version "13-ea" 2019-09-17

现在/usr/bin/java有了新的二进制文件,它的优先级高于$JAVA_HOME/bin/java

I have been confused with the same issue today after upgrade macOS to Big Sur.

There are two JDKs on my Mac, Oracle JDK 11 and Oracle JDK 8.

Then /usr/libexec/java_home always return /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home and ignore other arguments like -v 1.8

After some tests I found that, this only happened when your Mac have an Oracle JDK 1.8.X version.

So I deleted the Oracle JDK 1.8.x, /usr/libexec/java_home can return the correct JDK 11 path.

Then I tried AdoptOpenJDK 8, /usr/libexec/java_home can return its path correctly, but ignore the -v 11 arg, can't get the path of JDK 11.

It seems there ARE some bugs in java_home command, if you only have one JDK on your Mac - except Oracle JDK 8- it should works fine. If you have multiple JDKs, maybe it just return the last installed JDK path.

So either only keep one JDK installation or set $JAVA_HOME env with the absolute path will be OK.

I have added following on my .zshenv and it's working fine. Thanks munan.

## Java home
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_11_HOME=$(/usr/libexec/java_home -v11)

## Java alias
alias java8='export JAVA_VERSION=1.8'
alias java11='export JAVA_VERSION=11'

To change the java version from console, use: java8 or java11

in my opinion the command "/usr/libexec/java_home -v" doesn't work. I solved exporting directly the java path in file .bash_profile:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home

and then doing the command:

source .bash_profile.

After this using the command: echo $JAVA_HOME I've seen that the JVM path is set correctly.

UPDATE: Mac OS 13.1

When you install an openjdk version (in addition to the system/default installed), for example:

brew install openjdk@11

(in my case, at least) it is not linked to the right location by default. You need to symlink it as outlined here

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

This way when you run /usr/libexec/java_home -V , you will see all the installed versions

Matching Java Virtual Machines (2):
    19.0.1 (arm64) "Homebrew" - "OpenJDK 19.0.1" /opt/homebrew/Cellar/openjdk/19.0.1/libexec/openjdk.jdk/Contents/Home
    11.0.18 (arm64) "Homebrew" - "OpenJDK 11.0.18" /opt/homebrew/Cellar/openjdk@11/11.0.18/libexec/openjdk.jdk/Contents/Home

And choosing the right one is as simple as setting the env variable $JAVA_HOME

export JAVA_HOME=`/usr/libexec/java_home -v 11`  # to set to java 11, for eg

Running java --version confirms:

openjdk 11.0.18 2023-01-17
OpenJDK Runtime Environment Homebrew (build 11.0.18+0)
OpenJDK 64-Bit Server VM Homebrew (build 11.0.18+0, mixed mode)

I followed following steps to fix an issue to set java version I needed in macOS BigSur. :

when I execute this command

/usr/libexec/java_home -V

I was having the following versions:

Matching Java Virtual Machines (3):
17 (x86_64) "Homebrew" - "OpenJDK 17" /usr/local/Cellar/openjdk/17/libexec/openjdk.jdk/Contents/Home
11.0.17.0-m1 (x86_64) "IBM Corporation" - "IBM Semeru Runtime Open Edition 11" /Users/airasia/Library/Java/JavaVirtualMachines/semeru-11.0.17/Contents/Home
1.8.0_292 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

/usr/local/Cellar/openjdk/17/libexec/openjdk.jdk/Contents/Home

Then, I wanted to switched to java 1.8 as the default one, so I executed this command:

echo 'export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)' >> ~/.profile

and then when I checked java version:

java --version

I was still getting the old (higher one)

openjdk 17 2021-09-14
OpenJDK Runtime Environment Homebrew (build 17+0)
OpenJDK 64-Bit Server VM Homebrew (build 17+0, mixed mode, sharing)

~/.profile file was looking like,

vim ~/.profile
export JAVA_HOME=/usr/local/opt/openjdk/bin
export PATH=$JAVA_HOME/bin:$PATH

Then I checked

set | grep JAVA         
JAVA_HOME=JAVA_HOME=/usr/local/Cellar/openjdk/17/libexec/openjdk.jdk/Contents/Home

Then I added new JAVA_HOME in ~/.profile file,

vim ~/.profile
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
export PATH=$JAVA_HOME/bin:$PATH

And executed it,

source ~/.profile

Now java pointing to 1.8.0_292 ,

java -version
openjdk version "1.8.0_292"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_292-b10)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.292-b10, mixed mode)

To fallback to openjdk 17 again, I will just need to remove one line,

export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)

And add,

export JAVA_HOME=/usr/local/opt/openjdk/bin

And executed it,

source ~/.profile

Me too,So annoying, he caused me trouble. He seems to be solved by set the '$JAVA_VERSION'. Please use below command :-

export JAVA_VERSION=11

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM