How should I set the JAVA_HOME environment variable on macOS?

26,513

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

Share:
26,513
Marco Dinatsoli
Author by

Marco Dinatsoli

Updated on January 26, 2020

Comments

  • Marco Dinatsoli
    Marco Dinatsoli over 4 years

    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?

  • Marco Dinatsoli
    Marco Dinatsoli almost 9 years
    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 ?
  • ntalbs
    ntalbs almost 9 years
  • Marco Dinatsoli
    Marco Dinatsoli almost 9 years
    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/H‌​ome` does that mean that the java home variable is setted correctly now please?
  • ntalbs
    ntalbs almost 9 years
    Run java -version. If it work okay, than the variable is set correctly.
  • Marco Dinatsoli
    Marco Dinatsoli almost 9 years
    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?
  • ntalbs
    ntalbs almost 9 years
    Yes. Try set invlid path to JAVA_HOME and try run java command and see what happen.
  • Marco Dinatsoli
    Marco Dinatsoli almost 9 years
    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
    Marco Dinatsoli almost 9 years
    sorry for being panic, but we have finished right? now i can go on to continue installing things that need java home setted correctly?
  • Lance Kind
    Lance Kind over 4 years
    Theres a command for discovering JAVA_HOME: confessionsofanagilecoach.blogspot.com/2019/12/…