What should I set JAVA_HOME to on OSX

2019-01-01 14:12发布

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.

On OSX 10.6, the following paths seem to be valid for this variable

/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).

But which one should be used—or is it okay to use any of them?

13条回答
路过你的时光
2楼-- · 2019-01-01 14:20

Nowadays Java seems to be installed in /Library/Java/JavaVirtualMachines

查看更多
与风俱净
3楼-- · 2019-01-01 14:26

For me maven seems to work off the .mavenrc file:

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.

查看更多
何处买醉
4楼-- · 2019-01-01 14:29

As other answers note, the correct way to find the Java home directory is to use /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

查看更多
公子世无双
5楼-- · 2019-01-01 14:31

I'm on 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
查看更多
冷夜・残月
6楼-- · 2019-01-01 14:32

I am having MAC OS X(Sierra) 10.12.2.

I set JAVA_HOME to work on React Native(for Android apps) by following the following steps.

  • Open Terminal (Command+R, type Terminal, Hit ENTER).

  • Add the following lines to ~/.bash_profile. export JAVA_HOME=$(/usr/libexec/java_home)

  • Now run the following command. source ~/.bash_profile

  • You can check the exact value of JAVA_HOME by typing the following command. echo $JAVA_HOME

The value(output) returned will be something like below. /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home

That's it.

查看更多
与君花间醉酒
7楼-- · 2019-01-01 14:32

I tend to use /Library/Java/Home. The way the preferences pane works this should be up to date with your preferred version.

查看更多
登录 后发表回答