13

I know this question has been asked a lot before but i did read and i will show you what i got.

I will list the commands that i did in my OS X Yosemite 10.10.1

java -version

java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

java -fullversion

 java full version "1.8.0_45-b14"

which java

/usr/bin/java

Java home variable is not set because when I do this: echo $java_home or echo $JAVA_HOME or echo $Java_Home i got nothing on the terminal.

when doing this: ls -l which java i got this:

8 lrwxr-xr-x  1 root  wheel  74 Nov 12  2014 /usr/bin/java -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java

javac -version

javac 1.8.0_45

/usr/libexec/java_home

/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home

The solution that I think is correct after reading on internet is

echo "export JAVA_HOME=`/usr/libexec/java_home`" >> ~/.profile  

but i am afraid to test it, that is why i am asking you, if that is correct or not and if not what should i do please?

uchuugaka
  • 12,679
  • 6
  • 37
  • 55
Marco Dinatsoli
  • 10,322
  • 37
  • 139
  • 253

1 Answers1

34

I'm not sure why you are afraid to test, you can safely test this:

In your terminal session, input the following:

echo "export JAVA_HOME=`/usr/libexec/java_home`"

This will print the following line:

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

Copy the above and paste it into your terminal window, then press enter key. and execute java -version to see it work correctly. If everything is okay, you can attach the code into your .profile:

But adding this line directly is better idea because you don't have to update .profile when you upgrade JDK.

export JAVA_HOME=`/usr/libexec/java_home`

Please refer to the man page for the java_home tool. In short, it provides the appropriate path for JAVA_HOME for a normal installation on macOS

uchuugaka
  • 12,679
  • 6
  • 37
  • 55
ntalbs
  • 28,700
  • 8
  • 66
  • 83
  • so are you saying that i just need to do this `export JAVA_HOME=`/usr/libexec/java_home`` and then the java home variable is okay setted ? – Marco Dinatsoli May 26 '15 at 09:46
  • Yes. perhaps you want to read this: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/java_home.1.html – ntalbs May 26 '15 at 09:48
  • I did your code, and now when I did this `echo $JAVA_HOME` I got this ` /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home` does that mean that the java home variable is setted correctly now please? – Marco Dinatsoli May 26 '15 at 09:51
  • Run `java -version`. If it work okay, than the variable is set correctly. – ntalbs May 26 '15 at 09:53
  • by running java -version, i got this `java version "1.8.0_45" Java(TM) SE Runtime Environment (build 1.8.0_45-b14) Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)` so are we done please? – Marco Dinatsoli May 26 '15 at 09:54
  • Yes. Try set invlid path to JAVA_HOME and try run `java` command and see what happen. – ntalbs May 26 '15 at 09:59
  • when i set java home to a non existing folder, i got error message that file deson't exist. then i tried echo java home and nothing happened. then i re set the java home to the correct folder and re run echo java home and i got the correct path. – Marco Dinatsoli May 26 '15 at 10:01
  • sorry for being panic, but we have finished right? now i can go on to continue installing things that need java home setted correctly? – Marco Dinatsoli May 26 '15 at 10:08
  • Theres a command for discovering JAVA_HOME: https://confessionsofanagilecoach.blogspot.com/2019/12/setting-jdk-in-sts-4-on-macos.html – Lance Kind Dec 13 '19 at 21:32