简体   繁体   中英

Why command /usr/libexec/java_home doesn't work?

OS: MacOS Big Sur 11.0.1

I used the /usr/libexec/java_home command to change java version.

Some programs run need to special version. so I installed multi versions of java.

 /usr/libexec/java_home -V            
Matching Java Virtual Machines (7):
    14.0.1 (x86_64) "Oracle Corporation" - "Java SE 14.0.1" /Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home
    13 (x86_64) "Oracle Corporation" - "Java SE 13" /Library/Java/JavaVirtualMachines/jdk-13.jdk/Contents/Home
    10.0.2.0.13 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
    10.0.2 (x86_64) "Oracle Corporation" - "Java SE 10.0.2" /Library/Java/JavaVirtualMachines/jdk-10.0.2.jdk/Contents/Home
    1.8.0_192 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home
    1.8.0_111 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home
    1.7.0_80 (x86_64) "Oracle Corporation" - "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home

I configured the .bash_profile :

export JAVA_HOME=$(/usr/libexec/java_home)
export JAVA_7_HOME=$(/usr/libexec/java_home -v1.7)
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_10_HOME=$(/usr/libexec/java_home -v10)
export JAVA_13_HOME=$(/usr/libexec/java_home -v13)

alias jdk7="export JAVA_HOME=$JAVA_7_HOME"
alias jdk8="export JAVA_HOME=$JAVA_8_HOME"
alias jdk10="export JAVA_HOME=$JAVA_10_HOME"
alias jdk13="export JAVA_HOME=$JAVA_13_HOME"

export MAVEN_HOME=/Users/peizangpin/Program/apache-maven-3.6.3
export PATH=$PATH:$MAVEN_HOME/bin

export GRADLE_HOME=/Users/peizangpin/Program/gradle-6.7
export PATH=$PATH:$GRADLE_HOME/bin
export GRADLE_USER_HOME=/Users/peizangpin/Documents/ResuorcesPackage/gradle

export PATH=/opt/local/bin:$PATH
export PATH=/opt/local/sbin:$PATH
export PATH=/usr/local/bin:$PATH
export PATH=/usr/local/mysql/bin:$PATH
export PATH=$PATH:/usr/local/opt/go/libexec/bin
export PATH=$PATH:/usr/local/go/bin
export PATH=$PATH:/usr/local/opt/rabbitmq/sbin
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*

source ~/.bashrc

At least it was worked before the MacOS updated.

But now:

在此处输入图像描述

It always using the jdk14.

It's a problem for me.

Thanks for @MarioStefanutti shared link. Wrong JAVA_HOME after upgrade to macOS Big Sur v11.0.1

Since JAVA_HOME variable has been set. The /usr/libexec/java_home -v {pattern} will be useless.

So, the solution is to execute /usr/libexec/java_home -v {pattern} before the setting JAVA_HOME .

export JAVA_7_HOME=$(/usr/libexec/java_home -v1.7)
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_10_HOME=$(/usr/libexec/java_home -v10)
export JAVA_13_HOME=$(/usr/libexec/java_home -v13)

# finally set JAVA_HOME
export JAVA_HOME=$(/usr/libexec/java_home)
...

Since you say it worked before you updated MacOS, verify your shell is bash (the new default is zsh). Try something like echo $ZSH_NAME : If it is blank, you're running zsh instead of bash.

Try something like env JAVA_HOME=JAVA_13_HOME mvn... to verify your configuration.

Use /usr/libexec/java_home -v {version} before setting JAVA_HOME , otherwise it will always return the one set in JAVA_HOME

In shell initialization, order your statements to capture all the information needed through /usr/libexec/java_home -v {version} before setting JAVA_HOME

In other cases, or if you want to be absolutely sure, you may first unset JAVA_HOME before using /usr/libexec/java_home -v {version}

For example, when using something like direvn, a .envrc file would look like

unset JAVA_HOME
export JAVA_HOME=$(/usr/libexec/java_home -v 11)
PATH_add $JAVA_HOME/bin

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