为什么我在 MAC OS X 10.11 上看不到 JAVA_HOME 路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33046697/
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 14:13:20  来源:igfitidea点击:

Why I am not able to see the JAVA_HOME path on my MAC OS X 10.11?

javamacos

提问by Andy

I am trying to run a JAVA application on my MAC OS X 10.11 ( El Capitan).

我正在尝试在我的 MAC OS X 10.11 ( El Capitan ) 上运行 JAVA 应用程序。

I used this linkto set the JAVA_HOME on my MAC OS X 10.11.

我使用此链接在我的 MAC OS X 10.11 上设置 JAVA_HOME。

I am able to run the following commands:
java -version
which java

我能够运行以下命令:
java -version
which java

However when I go to the Terminal app on MAC and type echo $JAVA_HOME, I see nothing. Why is that? Does that mean that my JAVA_HOME is not set properly ?

但是,当我转到 MAC 上的终端应用程序并输入 echo $JAVA_HOME 时,我什么也没看到。这是为什么?这是否意味着我的 JAVA_HOME 设置不正确?

采纳答案by Markus W Mahlberg

Where to define it

在哪里定义

Ok, first of all, we have to make clear whereto set JAVA_HOME.

好的,首先我们要明确在哪里设置JAVA_HOME.

Simplified, you can define it in two files: either ~/.bashrcor ~/bash_profile. By defaultthe former is executed for what is called "interactive non-login shells" while the latter is used for "login shells". A "login shell" is exactly what you'd expect: a shell which is started after login via command line. An "interactive non-login shell" is a shell which is started from within a GUI for example. So, according to that, we should put our export statement into ~/.bashrc.

简化的,你可以在两个文件中定义它:要么~/.bashrc~/bash_profile。通过默认情况下前者是后者则用于“登录shell”所谓的“交互式非登录shell”执行。“登录外壳”正是您所期望的:通过命令行登录后启动的外壳。例如,“交互式非登录外壳”是从 GUI 内启动的外壳。因此,据此,我们应该将我们的出口声明放入~/.bashrc.

Side note: While OS X's "Terminal" application reads both files mentioned, this is not the default behavior and therefor should not be treated as such. And thats why I wrote an explanation.

旁注:虽然 OS X 的“终端”应用程序读取提到的两个文件,但这不是默认行为,因此不应如此对待。这就是为什么我写了一个解释。

What do define

定义什么

You need to export JAVA_HOME in the ~/.bashrcfile so that every time a shell is opened, the variable is set.

您需要在~/.bashrc文件中导出 JAVA_HOME,以便每次打开 shell 时都会设置该变量。

On OS X, the Java Development Kits and Runtime Environments are stored under /Library/Java/JavaVirtualMachines/for quite a while now. Have a look there. This is how it looks at my machine:

在 OS X 上,Java 开发工具包和运行时环境已经存储/Library/Java/JavaVirtualMachines/了很长时间。去那里看看。这是它如何看待我的机器:

/Library/Java/JavaVirtualMachines/
├── jdk1.7.0_45.jdk
├── jdk1.8.0_20.jdk
├── jdk1.8.0_25.jdk
└── jdk1.8.0_51.jdk

The subfolders look similar to this

子文件夹看起来与此类似

jdk1.7.0_45.jdk/
└── Contents
    ├── Home
    ├── Info.plist
    └── MacOS

And there we got it. So if you wanted to point to the JDK 1.7.0_45, you'd put the following statement into your .bashrc

我们得到了它。因此,如果您想指向 JDK 1.7.0_45,请将以下语句放入您的.bashrc

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

To make sure that the correct binaries for that Java version are called, you also should add the following somewhere after the above statement:

为了确保调用该 Java 版本的正确二进制文件,您还应该在上述语句后的某处添加以下内容:

export PATH=$JAVA_HOME/bin:$PATH

回答by Willie Z

If you want to use jdk 1.6 for some reason, which Apple has stopped supporting. It will be removed after you updated Mac OS. You can still get jdk 1.6 from this page: https://support.apple.com/kb/DL1572?locale=zh_CN

如果您出于某种原因想使用 jdk 1.6,Apple 已停止支持该版本。更新 Mac OS 后,它将被删除。您仍然可以从此页面获取 jdk 1.6:https: //support.apple.com/kb/DL1572?locale =zh_CN

After you install jdk 1.6 again, remember to confirm exporting the correct directory for system variable $JAVA_HOME in ~/.bash_profile, etc. For me, it is "/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home".

再次安装jdk 1.6后,记得在~/.bash_profile等中确认导出系统变量$JAVA_HOME的正确目录,对我来说是“/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home” .

回答by SDen

export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)

that is for Java 8 .... original link where I found it is here Installing Java on Mac OS X 10.10 Yosemite and 10.11 El Capitan

那是针对 Java 8 .... 我找到的原始链接在这里在 Mac OS X 10.10 Yosemite 和 10.11 El Capitan 上安装 Java

But the accepted answer works too. Just need to pay attention to path changes after updates.

但接受的答案也有效。只需要注意更新后的路径变化。

回答by chw1k

You will find many answers on how to set JAVA_HOME in OSX but it is hard to find an answer to the question if setting JAVA_HOME is necessary at all.

您会找到许多关于如何在 OSX 中设置 JAVA_HOME 的答案,但是如果根本需要设置 JAVA_HOME,则很难找到问题的答案。

Why is that? Does that mean that my JAVA_HOME is not set properly?

这是为什么?这是否意味着我的 JAVA_HOME 设置不正确?

On OSX, setting the JAVA_HOME environment variable is optional. The JRE and JDK installers do not provide for JAVA_HOME being set; so JAVA_HOME not being set is perfectly fine.

在 OSX 上,设置 JAVA_HOME 环境变量是可选的。JRE 和 JDK 安装程序不提供 JAVA_HOME 设置;所以 JAVA_HOME 没有被设置是完全没问题的。