Here is a useful little code snippet I use as part of BigSQL.

[bash]
if [ "x$JAVA_HOME" == "x" ]; then
macosx_j6="/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home"
ubuntu_j6="/usr/lib/jvm/java-6-openjdk-amd64"
redhat_j6="/usr/lib/jvm/java-1.6.0-openjdk.x86_64"
if [ -d "$macosx_j6" ]; then
export JAVA_HOME="$macosx_j6"
elif [ -d "$ubuntu_j6" ]; then
export JAVA_HOME="$ubuntu_j6"
elif [ -d "$redhat_j6" ]; then
export JAVA_HOME="$redhat_j6"
else
echo "WARNING: Cannot figure out JAVA_HOME."
exit 3
fi
fi
[/bash]