Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I set the JAVA_HOME environment variable on macOS? [duplicate]

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?

like image 828
Marco Dinatsoli Avatar asked May 26 '15 09:05

Marco Dinatsoli


1 Answers

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

like image 154
ntalbs Avatar answered Sep 18 '22 12:09

ntalbs