Setting the $JAVA_HOME environment variable on the Mac OS can be a little confusing. I hope this clears things up.
Open either ~/.bash_profile or ~/.profile in your favorite editor. I am using TextMate and I prefer editing ~/.profile.
Add “export JAVA_HOME=$(/usr/libexec/java_home)” to the file and save it.
You can reload the current environment without logging out by typing “. ~/.profile”
What is going on here? The java_home man page lays it out pretty clearly.
“The java_home command returns a path suitable for setting the JAVA_HOME environment variable. It determines this path from the user’s enabled and preferred JVMs in the Java Preferences application. Additional constraints may be provided to filter the list of JVMs available. By default, if no constraints match the available list of JVMs, the default order is used. The path is printed to standard output.”
There are serveral options called out in the man page but the most important to you is probably “-v” & “-V”
“/usr/libexec/java_home -V” – Prints the matching list of JVMs and architectures to stderr.
“/usr/libexec/java_home -v” – Filters the returned JVMs by the major platform version in “JVMVersion” form. Example versions: “1.5+”, or “1.6*”.
So, if you need to change JAVA_HOME to an earlier version of Java (maybe you have a program that requires Java 5), add “export JAVA_HOME=$(/usr/libexec/java_home -v 1.5)” to your ~/.profile file.
This assumes that version 1.5 was returned by “/usr/libexec/java_home -V”