bash 如何在 macOS 上设置 JAVA_HOME 环境变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32517507/
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
How to set JAVA_HOME environment variable on macOS?
提问by user1872384
According to this mobilefirst tutorial, it mentioned:
根据这个mobilefirst 教程,它提到:
You must have the JAVA_HOME environment variable set to your JDK directory.
您必须将 JAVA_HOME 环境变量设置为您的 JDK 目录。
For example:
例如:
Mac OSX: /Library/Java/JavaVirtualMachines/jdk1.7.0_67.jdk/Contents/Home
Mac OSX:/Library/Java/JavaVirtualMachines/jdk1.7.0_67.jdk/Contents/Home
I've added this 2 lines in .bash_profile:
我在 .bash_profile 中添加了这 2 行:
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
export PATH=$PATH:$JAVA_HOME/Contents/Commands
Is this correct?
这样对吗?
Thanks.
谢谢。
回答by Peter Salomonsen
in .bash_profile:
在 .bash_profile 中:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.xx/Contents/Home
回答by Mir Kazim Ali Tabrez
Adding the below answer to help those who are looking for step by step instructions on how to setup Java_Home on a Mac.
添加以下答案以帮助那些正在寻找有关如何在 Mac 上设置 Java_Home 的分步说明的人。
Determine whether Java is installed by using the command below:
which java
使用以下命令确定是否安装了 Java:
which java
You will something like this - /usr/bin/java
你会像这样 - /usr/bin/java
Next Step will be to determine the version of Java Installed by using the command below:
java -version
下一步将是使用以下命令确定安装的 Java 版本:
java -version
You will see something like java version "1.8.0_131"
您将看到类似 java 版本“1.8.0_131”的内容
Next step will be to get the location where the Java is installed:
cd /Library/Java
Under the Java folder, select the folder with the version that was displayed earlier:
/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home
Next check if Java_Home is setup to the correct location:
echo $JAVA_HOME
下一步将是获取安装 Java 的位置:
cd /Library/Java
在 Java 文件夹下,选择先前显示的版本所在的文件夹:
/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home
接下来检查 Java_Home 是否设置到正确的位置:
echo $JAVA_HOME
It will result in blank output if it is not already setup. If it returns the location, check if it points to the correct folder.
如果尚未设置,它将导致空白输出。如果它返回位置,请检查它是否指向正确的文件夹。
You can add or update the Java_Home using the below commands:
vi ~/.bash_profile
您可以使用以下命令添加或更新 Java_Home:
vi ~/.bash_profile
Navigate to the end of the file by pressing "Shift + g". Now press "i" to get to insert mode. Add the below lines in the bash_profile after replacing the path to the java home directory on your mac
按“Shift + g”导航到文件末尾。现在按“i”进入插入模式。替换 mac 上 java 主目录的路径后,在 bash_profile 中添加以下行
# Setting Java_Home
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home
Save this by pressing Esc followed by ":wq!"
通过按 Esc 后跟“:wq!”来保存它
Finally open a new terminal window and test the variable is setup correctly:
echo $JAVA_HOME
最后打开一个新的终端窗口并测试变量设置是否正确:
echo $JAVA_HOME
It should return the Java_Home path setup
它应该返回 Java_Home 路径设置
回答by chrisinmtown
Just a note to say this still works in OpenJDK version 11. As shown in the original question the java_home
utility lives in /usr/libexec
, which might not be on your $PATH
. You can run the command directly in a terminal window, here's sample output too:
只是说明这在 OpenJDK 版本 11 中仍然有效。如该java_home
实用程序所在的原始问题所示/usr/libexec
,它可能不在您的$PATH
. 您可以直接在终端窗口中运行该命令,这里也是示例输出:
$ /usr/libexec/java_home -v 11
/Library/Java/JavaVirtualMachines/jdk-11.0.4.jdk/Contents/Home
And set JAVA_HOME
to that value. Even better, wire your shell script's dot files as suggested in the original question by @user1872384, because that way your environment automatically keeps up with JDK upgrades as you install them.
并设置JAVA_HOME
为该值。更好的是,按照@user1872384 的原始问题中的建议连接您的 shell 脚本的点文件,因为这样您的环境会在您安装它们时自动跟上 JDK 升级。
回答by Jerry Chong
If your default terminal is /bin/zsh(Z Shell) like in my case, then you should set these environment variable in ~/.zshenvfile with following contents:
如果你的默认终端是/bin/zsh(Z Shell),就像我的例子一样,那么你应该在~/.zshenv文件中设置这些环境变量,内容如下:
export JAVA_HOME="$(/usr/libexec/java_home)"
Similarly, any other terminal type not mentioned above, you should set environment variable in its respective terminal env file.
同样,上面没有提到的任何其他终端类型,您应该在其各自的终端 env 文件中设置环境变量。
After saved the content into env file, restart terminal and call following commands:
将内容保存到 env 文件后,重新启动终端并调用以下命令:
echo $JAVA_HOME
It should display the full Java path.
它应该显示完整的 Java 路径。