我应该如何在 macOS 上设置 JAVA_HOME 环境变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30454505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:39:47  来源:igfitidea点击:

How should I set the JAVA_HOME environment variable on macOS?

javamacosjava-home

提问by Marco Dinatsoli

I know this question has been asked a lot before but i did read and i will show you what i got.

我知道这个问题以前被问过很多次,但我确实读过,我会告诉你我得到了什么。

I will list the commands that i did in my OS X Yosemite 10.10.1

我将列出我在OS X Yosemite 10.10.1中执行的命令

java -version

版本

java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

java -fullversion

java -fullversion

 java full version "1.8.0_45-b14"

which java

哪个java

/usr/bin/java

Java home variable is not set because when I do this: echo $java_homeor echo $JAVA_HOMEor echo $Java_Homei got nothing on the terminal.

Java home 变量未设置,因为当我这样做时:echo $java_homeecho $JAVA_HOMEecho $Java_Home我在终端上什么也没有。

when doing this: ls -l which javai got this:

这样做时: ls -lwhich java我得到了这个:

8 lrwxr-xr-x  1 root  wheel  74 Nov 12  2014 /usr/bin/java -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java

javac -version

javac -version

javac 1.8.0_45

/usr/libexec/java_home

/usr/libexec/java_home

/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home

The solution that I think is correct after reading on internet is

我在网上阅读后认为正确的解决方案是

echo "export JAVA_HOME=`/usr/libexec/java_home`" >> ~/.profile  

but i am afraid to test it, that is why i am asking you, if that is correct or not and if not what should i do please?

但我害怕测试它,这就是为什么我问你,如果这是否正确,如果不正确,我该怎么办?

采纳答案by ntalbs

I'm not sure why you are afraid to test, you can safely test this:

我不确定你为什么害怕测试,你可以安全地测试这个:

In your terminal session, input the following:

在终端会话中,输入以下内容:

echo "export JAVA_HOME=`/usr/libexec/java_home`"

This will print the following line:

这将打印以下行:

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

Copy the above and paste it into your terminal window, then press enter key. and execute java -versionto see it work correctly. If everything is okay, you can attach the code into your .profile:

复制上面的内容并将其粘贴到您的终端窗口中,然后按回车键。并执行java -version以查看它是否正常工作。如果一切正常,您可以将代码附加到您的.profile

But adding this line directly is better idea because you don't have to update .profilewhen you upgrade JDK.

但是直接添加这一行是更好的主意,因为您.profile在升级JDK时不必更新。

export JAVA_HOME=`/usr/libexec/java_home`

Please refer to the man page for the java_hometool. In short, it provides the appropriate path for JAVA_HOME for a normal installation on macOS

请参阅java_home工具的手册页。简而言之,它提供了 JAVA_HOME 的适当路径,以便在 macOS 上正常安装