I install Java .. a lot .. Simply put I love to tinker and I’m in charge of a few environments. One thing that’s always been sort of a thorn in my side is setting JAVA_HOME reliably. So, I went on a hunt and found a few ways to perform this seemingly simple task. Here is my pick for the best way… Yes, there’s a script.

(1) Create Permanent Profile Config Entry

cat > /etc/profile.d/java8.sh <<EOF 
export JAVA_HOME=$(dirname $(dirname $(readlink $(readlink $(which javac)))))
export PATH=\$PATH:\$JAVA_HOME/bin
export CLASSPATH=.:\$JAVA_HOME/jre/lib:\$JAVA_HOME/lib:\$JAVA_HOME/lib/tools.jar
EOF

(2) Source And Test

source /etc/profile.d/java8.sh

echo $JAVA_HOME

That’s it! I mostly stick to RHEL or CentOS, so I haven’t tried this on a Debian distro… However, if you’re feeling brave, it may very likely do the trick for some flavors of Ubuntu.

Cheers!