简体   繁体   中英

java -version does not change

I am having trouble changing the version of java running on my Mac running Big Sur 11.2 with zsh. I want to set Java 1.8 as my default, so I used the following commands to set JAVA_HOME:

$ unset JAVA_HOME
$ export JAVA_HOME=$(/usr/libexec/java_home -v "1.8.0_281")
$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home

However, when I ran java -version I kept getting JDK 15 as the current default. I then removed jdk15 from the JavaVirtualMachines directory and repeated the setting of JAVA_HOME, but I continue to observe the same result.

Other answers so far are talking about $PATH. This is a red herring; /usr/bin/java is on the path, it should remain on the path, and it is a light wrapper that goes to your actual, selected java installation.

The recommended strategy is to just use tools to solve this problem for you, such as jEnv . Install it via brew if you have it, otherwise, follow instructions on the site.

If you don't want to go the established route of using jEnv, the not-recommended route is, indeed, to mess with JAVA_HOME. /usr/bin/java will use whatever default rolls out of /usr/libexec/java_home . java_home will defer to what JAVA_HOME (the environment variable) says; only if it is unset will it give you its own default, which is generally the latest installed and very hard to change otherwise.

The fact that it isn't working for you suggests you have some different kind of shell, or that java version isn't listed by java_home , or you've messed with PATH in some very drastic ways (would involve removing security control from your mac disk and such), so I doubt that's it - doublecheck what you've done so far? Which mac version are you on?

An example from a mac:

/usr/libexec/java_home
> /Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home
java -version
> openjdk version "14.0.1" 2020-04-14

/usr/libexec/java_home -v 1.8
> /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
export JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

/usr/libexec/java_home
> /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
java -version
> openjdk version "1.8.0_252"

TLDR:

  1. echo $PATH and copy it's value
  2. export PATH=""
  3. export PATH="/path/from/step1/with/java/removed"

End TLDR

Had the same issue with Big Sur when I tried to switch to java 11 , despite everything pointing to correct values, java -version always returned java 8

$ 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)

$ whereis java             
/usr/bin/java

$ /usr/bin/java -version
openjdk version "11.0.11" 2021-04-20
OpenJDK Runtime Environment AdoptOpenJDK-11.0.11+9 (build 11.0.11+9)
OpenJDK 64-Bit Server VM AdoptOpenJDK-11.0.11+9 (build 11.0.11+9, mixed mode)

$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home

$ 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)

$ which java
/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/bin/java

To fix I had to remove Java SDK from PATH using steps in TLDR above

$ echo $PATH
/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/opt/X11/bin:/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home/bin:/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/bin:/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home/bin

$ export PATH=""
direnv: error can't find bash: exec: "bash": executable file not found in $PATH                                                                             

$ export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/opt/X11/bin"

$ echo $PATH    
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/opt/X11/bin

Afterwards

$ java -version 
openjdk version "11.0.11" 2021-04-20
OpenJDK Runtime Environment AdoptOpenJDK-11.0.11+9 (build 11.0.11+9)
OpenJDK 64-Bit Server VM AdoptOpenJDK-11.0.11+9 (build 11.0.11+9, mixed mode)

You should ensure that the right java version path come first in your PATH environmental variable

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