(Mac OS) JAVA版本未更改

huangapple go评论83阅读模式
英文:

(Mac OS) JAVA version does not change

问题

我在我的Macbook上有两个JAVA版本。我想把版本从14更改为11。我在互联网上找到了两种解决方案,但都不起作用。

我的 java -version 结果是

openjdk version "14.0.1" 2020-04-14
OpenJDK Runtime Environment (build 14.0.1+14)
OpenJDK 64-Bit Server VM (build 14.0.1+14, mixed mode, sharing)

第一个解决方案(https://stackoverflow.com/questions/21964709/how-to-set-or-change-the-default-java-jdk-version-on-os-x),
我运行 /usr/libexec/java_home -V 然后得到以下结果,

匹配的Java虚拟机(2):
14.0.1、x86_64:"OpenJDK 14.0.1"	/Library/Java/JavaVirtualMachines/openjdk.jdk/Contents/Home
11.0.8、x86_64:"Java SE 11.0.8"	/Library/Java/JavaVirtualMachines/jdk-11.0.8.jdk/Contents/Home

然后我执行了

export JAVA_HOME=`/usr/libexec/java_home -v 11.0.8`

java -version 进行检查后,结果仍然是14.0.1

第二个解决方案(https://stackoverflow.com/questions/14702702/how-to-set-java-home-in-mac-permanently), 我编辑了 ~/.bash_profile 文件如下,

export JAVA_HOME=`/usr/libexec/java_home -v 11.0.8`

然后我运行了命令

source ~/.bash_profile
echo $JAVA_HOME

它显示了结果,

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

但是当我用 java -version 检查结果时,仍然是14.0.1。

英文:

I have 2 JAVA version on my Macbook. I want to change version from 14 to 11. I found 2 solutions on the internet but both are not working.

My java -version result is

openjdk version "14.0.1" 2020-04-14
OpenJDK Runtime Environment (build 14.0.1+14)
OpenJDK 64-Bit Server VM (build 14.0.1+14, mixed mode, sharing)

1st Solution (https://stackoverflow.com/questions/21964709/how-to-set-or-change-the-default-java-jdk-version-on-os-x),
I run /usr/libexec/java_home -V then I got a result below,

Matching Java Virtual Machines (2):
14.0.1, x86_64:	"OpenJDK 14.0.1"	/Library/Java/JavaVirtualMachines/openjdk.jdk/Contents/Home
11.0.8, x86_64:	"Java SE 11.0.8"	/Library/Java/JavaVirtualMachines/jdk-11.0.8.jdk/Contents/Home

Then I did

export JAVA_HOME=`/usr/libexec/java_home -v 11.0.8`

After checking from java -version, The result is still 14.0.1

2nd solution (https://stackoverflow.com/questions/14702702/how-to-set-java-home-in-mac-permanently), I edit ~/.bash_profile file as below,

export JAVA_HOME=`/usr/libexec/java_home -v 11.0.8`

And I run command

source ~/.bash_profile
echo $JAVA_HOME

It shows the result,

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

But when I check the result with java -version, It's still 14.0.1 as well

答案1

得分: 8

安装 sdkman,它会处理那些相当繁琐的命令行操作,你必须使用这些操作来尝试使这成为可能。问题在于,JAVA_HOME 只是一个环境变量,它并不会改变任何东西 - 只有显式寻找它的工具(通常是 Maven 和 Ant)会受到对其进行更改的影响。当你在 macOS 上输入 java 时,它会运行 /usr/bin/java,这不是一个你可以随意更改的文件,即使是作为 root 用户也不行。然后,这个 java 会调用真正的 java,但不会查看 JAVA_HOME 来完成任务:它是一个指向 /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java 的符号链接,由于它位于 /System 中,你也无法更改它,即使是 root 用户也不行。

这就是为什么这么复杂,也是为什么你需要一个很好的工具(sdkman)来代替你进行这些操作。

英文:

Install sdkman which takes care of the rather tedious command line voodoo you have to employ to try to make this happen. The problem is, JAVA_HOME is just an environment variable, it changes nothing - only tools that explicitly look for it (generally, maven and ant for example) will be affected by messing with it. When you type java on a mac, it runs /usr/bin/java, which is not a file you can change even as root. This java will then invoke the real java, and does not look at JAVA_HOME to get the job done: It is a softlink to /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java, and because it is in /System you can't change that either, not even as root.

That's why this is so hard, and why you want a nice tool (sdkman) to do it for you.

答案2

得分: 5

export JAVA_HOME=/Library/Java/JavaVirtualMachines/openjdk.jdk/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH

英文:

The removal of quotes around the JAVA_HOME and then setting the path variable variable worked for me (Mac OS Catalina):

export JAVA_HOME=/Library/Java/JavaVirtualMachines/openjdk.jdk/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH

答案3

得分: 3

如果您需要在已安装JDK的不同版本之间进行切换,可以通过在bash配置文件中设置别名来重置JAVA_HOME并重新初始化PATH

export JAVA_11_HOME=$(/usr/libexec/java_home -v11)
export JAVA_14_HOME=$(/usr/libexec/java_home -v14)

alias java11='export JAVA_HOME=$JAVA_11_HOME; export PATH=$JAVA_HOME/bin:$PATH'
alias java14='export JAVA_HOME=$JAVA_14_HOME; export PATH=$JAVA_HOME/bin:$PATH'

然后,在重新启动终端会话后,您可以来回切换:

$ java11
$ java -version

$ java14
$ java -version
英文:

If you need to switch between versions and already have the JDKs installed, you can adjust your bash profile with aliases that reset JAVA_HOME and reinitialize your PATH each time:

export JAVA_11_HOME=$(/usr/libexec/java_home -v11)
export JAVA_14_HOME=$(/usr/libexec/java_home -v14)

alias java11='export JAVA_HOME=$JAVA_11_HOME; export PATH=$JAVA_HOME/bin:$PATH'
alias java14='export JAVA_HOME=$JAVA_14_HOME; export PATH=$JAVA_HOME/bin:$PATH'

Then, after restarting your terminal session, you can switch back and forth:

$ java11
$ java -version

and

$ java14
$ java -version

答案4

得分: 0

尝试执行以下命令:export JAVA_HOME='/usr/libexec/java_home -v 11'(不包括小版本号),这在我的 Mac 上输出了正确的 java -version。

英文:

Try export JAVA_HOME='/usr/libexec/java_home -v 11' (without the minor version), it outputs the right java -version for me on Mac.

huangapple
  • 本文由 发表于 2020年10月13日 21:07:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64335935.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定