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

References

  1. Important Java Directories on Mac OS X
  2. What should I set JAVA_HOME to on OSX

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
31 Comment threads
5 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
31 Comment authors
Tuan D. NguyenIdiom masterMikeSarunyoo KeawsopaAlexander Fauske Recent comment authors
newest oldest most voted
Mike
Guest
Mike

Your site is one of the best out there for development. Thanks for what you provide.

Michael Hasse
Guest
Michael Hasse

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

Tuan D. Nguyen
Guest
Tuan D. Nguyen

Awesome tips

Idiom master
Guest
Idiom master

Nice!

Sarunyoo Keawsopa
Guest
Sarunyoo Keawsopa

??????????
???? ???????? ????????????

Alexander Fauske
Guest
Alexander Fauske

Only your solution worked for me after unsuccessfully digging around the Internet for some time.
Thanks a lot.

Raghuram Pulijala
Guest
Raghuram Pulijala

Works like a charm.. thanks a lot.. have been following your blog for quite sometime… kudos on your spot on and precise solutions…

Ewen Mackenzie
Guest
Ewen Mackenzie

I need a favorite here please

bo
Guest
bo

awesome source!

Pooja
Guest
Pooja

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]

Pooja
Guest
Pooja

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]

BH
Guest
BH

Thank you! It helps.

Mike Mainguy
Guest
Mike Mainguy

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

Abel Caim
Guest
Abel Caim

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#

Abel Caim
Guest
Abel Caim

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

Lamont Granquist
Guest
Lamont Granquist

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

yonas
Guest
yonas

This is excellent and to the point. Thanks so much!

Jt
Guest
Jt

This page has helped me numerous time. thanks mkyong

javja
Guest
javja

Thanks it helped me and saved my time.

jt
Guest
jt

Thank you

Fernando
Guest
Fernando

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.

Benji X.
Guest
Benji X.

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.

Harry
Guest
Harry

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.

yonas
Guest
yonas

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.

ub10
Guest
ub10

You have to type the command: source ~/.bash_profile, everytime you open a new terminal, for the path to be set in that session.

eunae
Guest
eunae

Thank you! very helpful.

Julien L
Guest
Julien L

Thanks a lot for these clear, precise and straightforward explanations.

Jason Zwolak
Guest
Jason Zwolak

Thank you! Another post on your blog that has helped me a lot.

trackback
Lab D0105a: Instalación de Openbravo en Mac OS X 10.9 | I'm a beginner

[…] 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 […]

josesaid
Guest
josesaid

Thanks dude, it saved me too much time and effort.

Kevin
Guest
Kevin

Thank you!

D Mehta
Guest
D Mehta

Very nice, helpful. Thanks.

Alonso
Guest
Alonso

Thanks a lot, you are the best man!

Nilesh
Guest
Nilesh

Thanks a lot. Always find your site useful.

richard
Guest
richard

Hi colleagues, its enormous article about educationand entirely explained, keep it up
all the time.