How to Set $JAVA_HOME environment variable on Mac OS X
In this tutorial, we show you how to set $JAVA_HOME environment variable on latest or older Mac OSX.
1. Mac OSX 10.5 or later
In Mac OSX 10.5 or later, Apple recommends to set the $JAVA_HOME variable to /usr/libexec/java_home, just export $JAVA_HOME in file ~/. bash_profile or ~/.profile.
$ vim .bash_profile
export JAVA_HOME=$(/usr/libexec/java_home)
$ source .bash_profile
$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home
Why /usr/libexec/java_home?
This java_home can return the Java version specified in Java Preferences for the current user. For examples,
/usr/libexec/java_home -V
Matching Java Virtual Machines (3):
1.7.0_05, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home
1.6.0_41-b02-445, x86_64: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
1.6.0_41-b02-445, i386: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
This Mac OSX has three JDK installed.
##return top Java version
$ /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home
## I want Java version 1.6
$ /usr/libexec/java_home -v 1.6
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
2. Older Mac OSX
For older Mac OSX, the /usr/libexec/java_home doesn’t exists, so, you should set JAVA_HOME to the fixed path :
$ vim .bash_profile
export JAVA_HOME=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
$ source .bash_profile
$ echo $JAVA_HOME
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Comments
hi all – i tried the above sequence of steps – below output:
Michaels-MacBook-Pro:~ mhasse$ $ vim .bash_profile
-bash: $: command not found
Michaels-MacBook-Pro:~ mhasse$ export JAVA_HOME=$(/usr/libexec/java_home)
-bash: /usr/libexec/java_home: No such file or directory
Michaels-MacBook-Pro:~ mhasse$ $ echo $JAVA_HOME
-bash: $: command not found
Your site is one of the best out there for development. Thanks for what you provide.
Thanks a lot. Always find your site useful.
Doesn’t work. Well it does but it sets JAVA_HOME only for specific terminal. When I open new tab in the terminal and write mvn –version it still shows 1.6 and echo $JAVA_HOME doesn’t show anything at all.
Make sure you are saving this in your bash profile. if you just type it into your terminal, it will not persist after you close the terminal.
I have a similar problem – I typed this into my ~/.bash_profile, but using the source command doesn’t persist in another shell. When I use $cat ~/.bash_profile it prints out the correct contents.
Did you try restarting Terminal altogether? Sometimes there is some weird funniness with session persistence. I always restart Terminal after editing .bash_profile just in case.
You have to type the command: source ~/.bash_profile, everytime you open a new terminal, for the path to be set in that session.
Thanks dude, it saved me too much time and effort.
Thank you! Another post on your blog that has helped me a lot.
Thanks a lot for these clear, precise and straightforward explanations.
Thank you! very helpful.
Thanks it helped me and saved my time.
Thanks a lot!
Hi colleagues, its enormous article about educationand entirely explained, keep it up
all the time.
Thanks a lot, you are the best man!
Very nice, helpful. Thanks.
Thank you!
[…] de la variable de entorno JAVA_HOME se puede hacer siguiendo los consejos de esta página: http://www.mkyong.com/java/how-to-set-java_home-environment-variable-on-mac-os-x/ y la realizaremos a través de uno de los ficheros .sh que alojaremos en profile.d de esta […]
This page has helped me numerous time. thanks mkyong
This is excellent and to the point. Thanks so much!
Virus:bin root# java -version
java version “1.8.0_25”
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)
Virus:bin root# whereis java|xargs ls -ltr
lrwxr-xr-x 1 root wheel 73 Nov 25 14:35 /usr/bin/java -> /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java
Virus:bin root#
awesome source!
I need a favorite here please
Works like a charm.. thanks a lot.. have been following your blog for quite sometime… kudos on your spot on and precise solutions…
Only your solution worked for me after unsuccessfully digging around the Internet for some time.
Thanks a lot.
Nice!
Thank you
To whom is interested to fix it up.
1) whereis java | xargs rm -rf
2) ln -s “/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java” /usr/bin/java
3) java -version
export JAVA_HOME=”/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home”
is probably a better solution that doesn’t require surgery to /usr/bin/java
And to enhance it somewhat, you can add a shell function to quickly change between java6,7,8 for testing. http://mikemainguy.blogspot.com/2014/11/easily-changing-java-versions-in-osx.html
Thank you! It helps.
Thanks for posting this- I want to know how to get the value of env variable in java code, because
System.getenv(“JAVA_HOME”) returns null [though it works for windows machine]
Thanks for posting this- is there any descent way to get the value of env variable in java code, because
System.getenv(“JAVA_HOME”) returns null [though it works for windows machine]
Awesome tips