[Dev] – How to set up “$JAVA_HOME” environment variable on Mac OS X?

For Mac OSX => 10.5

  • Open the bash_profile file to edit it.
    $ vim ~/.bash_profile
    
  • Add into the ~/.bash_profile the following line
 export JAVA_HOME=$(/usr/libexec/java_home)
  • Perform the changes
$ source ~/.bash_profile
  • Check if it was made with success
$ echo $JAVA_HOME

You can choose the version of JAVA via “/usr/libexec/java_home”

The following are some examples of this.

/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

For Mac OSX < 10.5

For older versions that 10.5 is required defined the path. The following the steps with an example of path.

  • Open the bash_profile file to edit it.
    $ vim ~/.bash_profile
    
  • Add into the ~/.bash_profile the following line
    export JAVA_HOME="/System/Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home"
  • Perform the changes
$ source ~/.bash_profile
  • Check if it was made with success
$ echo $JAVA_HOME

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s