如何在 Mac OS X 10.9 上设置 JAVA_HOME 环境变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22842743/
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 Mac OS X 10.9?
提问by islander_zero
I just purchased a brand new MacBook Pro.
我刚刚购买了全新的 MacBook Pro。
This is my first MAC ever and I'm still trying to get the hang of navigating my way around.
这是我有史以来的第一个 MAC,我仍在努力掌握导航的窍门。
Anyway, I'm also new to Java and I've been practicing on my Windows PC before it permanently died.
无论如何,我也是 Java 的新手,并且在它永久死亡之前我一直在我的 Windows PC 上练习。
Now that I'm on this MAC, I installed my JDK and now I need to set the JAVA_HOME
environment variable.
现在我在这个 MAC 上,我安装了我的 JDK,现在我需要设置JAVA_HOME
环境变量。
I have no idea what to do.
我不知道该怎么做。
I tried following some of these guides and didn't get very far.
我尝试遵循其中一些指南,但并没有走多远。
I was able to locate the terminal and I think I created some multiple files. I'm getting messages like this:
我能够找到终端,我想我创建了一些多个文件。我收到这样的消息:
(1) Another program may be editing the same file. If this is the case, be careful not to end up with two different instances of the same file when making changes. Quit, or continue with caution.
(1) 另一个程序可能正在编辑同一个文件。如果是这种情况,请注意不要在进行更改时以同一文件的两个不同实例结束。退出,或谨慎继续。
(2) An edit session for this file crashed. If this is the case, use ":recover" or "vim -r /Users/Erwin/.bash_profile" to recover the changes (see ":help recovery"). If you did this already, delete the swap file "/Users/Erwin/.bash_profile.sw p" to avoid this message.
(2) 此文件的编辑会话崩溃。如果是这种情况,请使用 ":recover" 或 "vim -r /Users/Erwin/.bash_profile" 来恢复更改(参见 ":help recovery")。如果您已经这样做了,请删除交换文件“/Users/Erwin/.bash_profile.sw p”以避免出现此消息。
Can somebody tell how to set Java in Mac OSX environment step by step?
有人可以告诉如何在 Mac OSX 环境中逐步设置 Java 吗?
采纳答案by Adrian Petrescu
Literally all you have to do is:
从字面上看,您所要做的就是:
echo export "JAVA_HOME=$(/usr/libexec/java_home)" >> ~/.bash_profile
and restart your shell.
并重新启动您的外壳。
If you have multiple JDK versions installed and you want it to be a specific one, you can use the -v
flag to java_home
like so:
如果您安装了多个 JDK 版本,并且希望它是一个特定版本,则可以使用该-v
标志来表示java_home
:
echo export "JAVA_HOME=$(/usr/libexec/java_home -v 1.7)" >> ~/.bash_profile
回答by JohnL4
I did it by putting
我通过把它做到了
export JAVA_HOME=`/usr/libexec/java_home`
(backtics) in my .bashrc. See my comment on Adrian's answer.
(backtics) 在我的 .bashrc 中。请参阅我对阿德里安回答的评论。
回答by Senthil Acs
I got it working by adding to ~/.profile. Somehow after updating to El Capitan beta, it didnt work even though JAVA_HOME was defined in .bash_profile.
我通过添加到 ~/.profile 使其工作。不知何故更新到 El Capitan 测试版后,即使 JAVA_HOME 是在 .bash_profile 中定义的,它也不起作用。
If there are any El Capitan beta users, try adding to .profile
如果有 El Capitan 测试版用户,请尝试添加到 .profile
回答by Rajesh D
In Mac OSX 10.5 or later, Apple recommends to set the $JAVA_HOME variable to /usr/libexec/java_home
, just export $JAVA_HOME
in file ~/. bash_profile
or ~/.profile
.
在 Mac OSX 10.5 或更高版本中,Apple 建议设置$JAVA_HOME variable to /usr/libexec/java_home
, just export$JAVA_HOME
in file ~/. bash_profile
或~/.profile
.
Open the terminal and run the below command.
打开终端并运行以下命令。
$ vim .bash_profile
export JAVA_HOME=$(/usr/libexec/java_home)
save and exit from vim editor, then run the source command on .bash_profile
保存并退出 vim 编辑器,然后在 .bash_profile 上运行 source 命令
$ source .bash_profile
$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home
回答by DoctorPangloss
I've updated the great utility jenv
to make it easy to setup on macOS.
我已经更新了这个很棒的实用程序jenv
,以便在 macOS 上轻松设置。
Follow the instructions on https://github.com/hiddenswitch/jenv
按照https://github.com/hiddenswitch/jenv上的说明进行操作
回答by Shomu
Set $JAVA_HOMEenvironment variable on latest or older Mac OSX.
在最新或更旧的 Mac OSX 上设置$JAVA_HOME环境变量。
Download & Install install JDK
下载并安装安装JDK
- First, install JDK
- Open terminal check java version
- 一、安装JDK
- 打开终端检查java版本
$ java -version
$ java -version
Set JAVA_HOME environment variable
设置 JAVA_HOME 环境变量
- Open .zprofilefile
- 打开.zprofile文件
$ open -t .zprofile
$ open -t .zprofile
Or create . zprofilefile
或创建. zprofile文件
$
open -t .zprofile
$
open -t .zprofile
- write in .zprofile
- 写入 .zprofile
export JAVA_HOME=$(/usr/libexec/java_home)
export JAVA_HOME=$(/usr/libexec/java_home)
Save .zprofileand close the bash file & then write in the terminal for work perfectly.
保存 .zprofile并关闭 bash 文件,然后在终端中写入以完美工作。
$ source .zprofile
Setup test in terminal
在终端设置测试
$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk-13.0.1.jdk/Contents/Home
回答by Jerry Chong
It is recommended to check default terminal shellbefore set JAVA_HOMEenvironment variable, via following commands:
建议在设置JAVA_HOME环境变量之前检查默认终端 shell,通过以下命令:
$ echo $SHELL
/bin/bash
If your default terminal is /bin/bash(Bash), then you should use @Adrian Petrescumethod.
如果您的默认终端是/bin/bash(Bash),那么您应该使用@Adrian Petrescu方法。
If your default terminal is /bin/zsh(Z Shell), 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 文件中设置环境变量。