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:36

OSX Yosemite, ZSH, and Java SE Runtime Environment 8, I had to:

$ sudo ln -s /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands /System/Library/Frameworks/JavaVM.framework/Versions/Current/bin

and in ~/.zshrc change JAVA_HOME to export JAVA_HOME="/System/Library/Frameworks/JavaVM.framework/Versions/Current"

查看更多
裙下三千臣
3楼-- · 2019-01-01 06:36

for macOS Mojave 10.14.1 and JAVA 11.0.1 I set the profile as

export JAVA_HOME=$(/usr/libexec/java_home)

key in terminal this to confirm:

$JAVA_HOME/bin/java -version

java version "11.0.1" 2018-10-16 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)
查看更多
冷夜・残月
4楼-- · 2019-01-01 06:38

The above didn't work for me with Amazon's EC2 tools, because it expects bin/java etc. underneath JAVA_HOME. /System/Library/Frameworks/JavaVM.framework/Home did work.

查看更多
听够珍惜
5楼-- · 2019-01-01 06:41

If you are in need to have multiple versions of JDK under Mac OS X (Yosemite), it might be helpful to add some scripting for automated switching between them.

What you do is to edit your ~/.bash_profile and add the following:

function setjdk() {
  if [ $# -ne 0 ]; then
   removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin'
   if [ -n "${JAVA_HOME+x}" ]; then
    removeFromPath $JAVA_HOME
   fi
   export JAVA_HOME=`/usr/libexec/java_home -v $@`
   export PATH=$JAVA_HOME/bin:$PATH
  fi
 }
 function removeFromPath() {
  export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")
 }
setjdk 1.7

What the script does is to first remove other JDK versions in the PATH so that they won’t interfere with our new JDK version. Then it makes some clever use of /usr/libexec/java_home which is a command that lists installed JDK versions. The -v argument tells java_home to return the path of the JDK with the supplied version, for example 1.7. We also update the PATH to point to the bin directory of the newly found JAVA_HOME directory. At the end we can simply execute the function using

setjdk 1.7

which selects the latest installed JDK version of the 1.7 branch. To select a specific version you can simply execute

setjdk 1.7.0_51

instead. Run /usr/libexec/java_home -V to get more details on how to choose versions.

P.S. Do not forget to source ~/.bash_profile after you save it.

查看更多
伤终究还是伤i
6楼-- · 2019-01-01 06:42

On Mac OS X Lion, to set visualgc to run, I used:

export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home
查看更多
春风洒进眼中
7楼-- · 2019-01-01 06:42

Just set java_home of 1.8 jdk version in netbeans.conf file:

/Applications/NetBeans/NetBeans 8.2.app/Contents/Resources/NetBeans/etc/netbeans.conf

uncomment line:

netbeans_jdkhome="path/to/jdk"

and set path to your 1.8 jdk, in my case:

netbeans_jdkhome="/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home"

This approach lays you to have several jdk versions on mac os

查看更多
登录 后发表回答