Where is JAVA_HOME on macOS Mojave (10.14) to Lion

2019-01-01 06:10发布

Java is an optional package on the latest versions of macOS.

Yet once installed it appears like the JAVA_HOME environment variable is not set properly.

标签: java macos
24条回答
栀子花@的思念
2楼-- · 2019-01-01 06:23

For OS X you can do:

export JAVA_HOME=`/System/Library/Frameworks/JavaVM.framework/Versions/Current/commands/java_home`
查看更多
看风景的人
3楼-- · 2019-01-01 06:23

This answer is related to Mountain Lion and not Lion. I needed to do this for the AWS Command Line Tools. According to the AWS docs, running which java returns /usr/bin/java.

So, I set JAVA_HOME=/usr in my .bashrc.

Apparently, /usr/bin/java is a symbolic link to /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java which makes it all work.

Update

As mentioned in the comment below, this JAVA_HOME value is not an ideal solution when the JAVA_HOME environment variable is to be used by things other than the AWS Command Line Tools. It works fine for the AWS Command Line Tools, though, as given in their docs.

查看更多
长期被迫恋爱
4楼-- · 2019-01-01 06:25

Newer Oracle JVMs such as 1.7.0_21-b12 seem to install here:

/Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home

查看更多
十年一品温如言
5楼-- · 2019-01-01 06:27

With the Java optional package or Oracle JDK installed, adding one of the following lines to your ~/.bash_profile file will set the environment variable accordingly.

export JAVA_HOME="$(/usr/libexec/java_home -v 1.6)"
or
export JAVA_HOME="$(/usr/libexec/java_home -v 1.7)"
or
export JAVA_HOME="$(/usr/libexec/java_home -v 1.8)"

Update: added -v flag based on Jilles van Gurp response.

查看更多
十年一品温如言
6楼-- · 2019-01-01 06:27

/usr/libexec/java_home is not a directory but an executable. It outputs the currently configured JAVA_HOME and doesn't actually change it. That's what the Java Preferences app is for, which in my case seems broken and doesn't actually change the JVM correctly. It does list the 1.7 JVM but I can toggle/untoggle & drag and drop all I want there without actually changing the output of /usr/libexec/java_home.

Even after installing 1.7.0 u6 from Oracle on Lion and setting it as the default in the preferences, it still returned the apple 1.6 java home. The only fix that actually works for me is setting JAVA_HOME manually:

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

At least this way when run from the command line it will use 1.7. /usr/libexec/java_home still insists on 1.6.

Update: Understanding Java From Command Line on OSX has a better explanation on how this works.

export JAVA_HOME=`/usr/libexec/java_home -v 1.7` 

is the way to do it. Note, updating this to 1.8 works just fine.

查看更多
姐姐魅力值爆表
7楼-- · 2019-01-01 06:28

A better (more upgradable) way is to use the following:

/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home

This should work with AWS also since it has bin underneath Home

查看更多
登录 后发表回答