我应该在 macOS X 10.6 上设置 JAVA_HOME 环境变量吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1348842/
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
What should I set JAVA_HOME environment variable on macOS X 10.6?
提问by Robert Christie
Many Java applications that use shell scripts to configure their environment use the JAVA_HOME
environment variable to start the correct version of Java, locate JRE JARs, and so on.
许多使用 shell 脚本配置其环境的 Java 应用程序使用JAVA_HOME
环境变量来启动正确版本的 Java、定位 JRE JAR 等。
In macOS X 10.6, the following paths seem to be valid for this variable
在 macOS X 10.6 中,以下路径似乎对该变量有效
/Library/Java/Home
/System/Library/Frameworks/JavaVM.framework/Home
/System/Library/Frameworks/JavaVM.framework/Versions/Current
Some of these are symlinks to the actual current VM (as defined in the Java Preference pane).
其中一些是指向实际当前 VM(在 Java 首选项窗格中定义)的符号链接。
But which one should be used—or is it okay to use any of them?
但是应该使用哪个 - 或者可以使用其中的任何一个?
采纳答案by mipadi
I just set JAVA_HOME
to the output of that command, which should give you the Java path specified in your Java preferences. Here's a snippet from my .bashrc
file, which sets this variable:
我只是设置JAVA_HOME
为该命令的输出,它应该为您提供 Java 首选项中指定的 Java 路径。这是我的.bashrc
文件中的一个片段,它设置了这个变量:
export JAVA_HOME=$(/usr/libexec/java_home)
I haven't experienced any problems with that technique.
我没有遇到任何使用该技术的问题。
Occasionally I do have to change the value of JAVA_HOME
to an earlier version of Java. For example, one program I'm maintaining requires 32-bit Java 5 on OS X, so when using that program, I set JAVA_HOME
by running:
有时,我必须将 的值更改JAVA_HOME
为 Java 的早期版本。例如,我维护的一个程序在 OS X 上需要 32 位 Java 5,因此在使用该程序时,我JAVA_HOME
通过运行进行设置:
export JAVA_HOME=$(/usr/libexec/java_home -v 1.5)
For those of you who don't have java_home
in your path add it like this.
对于那些没有java_home
在你的路径中的人,像这样添加它。
sudo ln -s /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java_home /usr/libexec/java_home
References:
参考资料:
回答by Clint
I tend to use /Library/Java/Home
. The way the preferences pane works this should be up to date with your preferred version.
我倾向于使用/Library/Java/Home
. 首选项窗格的工作方式应该与您的首选版本保持同步。
回答by lucasarruda
Also, it`s interesting to set your PATH to reflect the JDK. After adding JAVA_HOME (which can be done with the example cited by 'mipadi'):
此外,设置 PATH 以反映 JDK 很有趣。添加 JAVA_HOME 后(可以用'mipadi'引用的例子来完成):
export JAVA_HOME=$(/usr/libexec/java_home)
export JAVA_HOME=$(/usr/libexec/java_home)
Add also in ~/.profile:
在 ~/.profile 中添加:
export PATH=${JAVA_HOME}/bin:$PATH
export PATH=${JAVA_HOME}/bin:$PATH
P.S.:For OSX, I generally use .profile in the HOME dir instead of .bashrc
PS:对于 OSX,我通常在 HOME 目录中使用 .profile 而不是 .bashrc
回答by Nicolas Mommaerts
Nowadays Java seems to be installed in /Library/Java/JavaVirtualMachines
现在Java似乎安装在 /Library/Java/JavaVirtualMachines
回答by Rae
I'm on Mac OS 10.6.8
我使用的是 Mac OS 10.6.8
The easiest solution works for me is simply put in
对我有用的最简单的解决方案是简单地放入
$ export JAVA_HOME=$(/usr/libexec/java_home)
To test whether it works, put in
要测试它是否有效,请输入
$ echo $JAVA_HOME
it shows
表明
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
you can also test
你也可以测试
$ which java
回答by Pálossy László
That above works not any more in YOSEMITE for GRAPHICAL APPLICATIONS! Like eclipse, or anything started with Spotlight. (.bash_profile, launchd.conf works for terminal sessions only.) Before starting eclipse, just open a terminal window, and give out the following command:
以上不再适用于优胜美地的图形应用程序!像 eclipse,或任何以 Spotlight 开始的东西。(.bash_profile,launchd.conf 仅适用于终端会话。)在启动 eclipse 之前,只需打开一个终端窗口,并发出以下命令:
launchctl setenv JAVA_HOME /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home
(With your installation path! Perhaps works with $(/usr/libexec/java_home) instead of the full path too.)
(使用您的安装路径!也许也适用于 $(/usr/libexec/java_home) 而不是完整路径。)
View the whole excellent article about the permanent solution here: Setting environment variables via launchd.conf no longer works in OS X Yosemite/El Capitan/macOS Sierra?
在此处查看有关永久解决方案的整篇优秀文章: 通过 launchd.conf 设置环境变量不再适用于 OS X Yosemite/El Capitan/macOS Sierra?
回答by Shirow
I've found this stackto help, i was having the same issue and i could fix:
我发现这个堆栈有帮助,我遇到了同样的问题,我可以解决:
My java path was here:
我的java路径在这里:
/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
and was needed to put into my .bash_profile
:
并且需要放入我的.bash_profile
:
export JAVA_HOME=\"/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home\"
export JAVA_HOME=\"/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home\"
Hope help
希望帮助
回答by Kristopher Johnson
As other answers note, the correct way to find the Java home directory is to use /usr/libexec/java_home
.
正如其他答案所指出的,找到 Java 主目录的正确方法是使用/usr/libexec/java_home
.
The official documentation for this is in Apple's Technical Q&A QA1170: Important Java Directories on OS X: https://developer.apple.com/library/mac/qa/qa1170/_index.html
对此的官方文档位于 Apple 的技术问答 QA1170:OS X 上的重要 Java 目录:https: //developer.apple.com/library/mac/qa/qa1170/_index.html
回答by Yichuan Wang
For Fish Shell users, use something like the following: alias java7 "set -gx JAVA_HOME (/usr/libexec/java_home -v1.7)"
对于 Fish Shell 用户,请使用以下内容: alias java7 "set -gx JAVA_HOME (/usr/libexec/java_home -v1.7)"
回答by maksimov
For me maven seems to work off the .mavenrc
file:
对我来说,Maven 似乎可以关闭.mavenrc
文件:
echo "export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)" > ~/.mavenrc
I'm sure I picked it up on SO too, just can't remember where.
我确定我也是在 SO 上捡到的,只是不记得在哪里。