There are two parts to this problem - figuring out where Java is installed in your system and then setting up the environment variable in a reliable way so its always available.
There is a command you can use to find the actual path of Java on your system:
/usr/libexec/java_home
on my system this gives:
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
on your system it may be different and of course it will change with every release of Java installed.
Apple recommends in this article that instead of referencing this path directly you point your Java_Home environment variable to this symbolic link instead:
/Library/Java/Home
This link actually points to a chain of links which ultimately end up in the /System/Library/Java... path above.
So that's step one sorted, what about step two: setting up the environment variable?
A discussion here seems to favour setting the variable in /etc/launchd.conf, the reasoning for this is made clear in the discussion. I took their word for it, and it worked for me, if your happy to do the same go ahead and edit the following file:
/etc/launchd.conf
this file may not exist already on your system, in which case editing it will cause it to be created.
Add this line:
setenv JAVA_HOME /Library/Java/Home
That should do it, but you'll need to restart your system for the change to take effect.
After the restart open up a terminal and enter the following command to see a list of the currently defined environment variables:
export
you should see JAVA_HOME listed and the value should be /Library/Java/Home.
if you want to refer to the environment variable in scripts or from the terminal you need to prepend $ to the environment variable, for example:
echo $JAVA_HOME
will display the value of the variable, while the following will take you to the Java directory:
cd $JAVA_HOME
Well that's two hours of my life i'm not getting back - I hope this post saved you a bit of yours!
Thank you for sharing this information. Is there any way to put '/usr/libexec/java_home' directly on the launchd?
ReplyDelete