Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I wonder whether Ubuntu itself provides a system-wide way to find the current JAVA_HOME or whether there is right place for setting JAVA_HOME.

Mac OS X does well in this, for /usr/libexec/java_home command returns the current JAVA_HOME. But to Ubuntu, the materials I found always told me to set the $JAVA_PATH pointing to a static path like ```, which is stored in~/.bash_profile```.

I think, however, this approach has several drawbacks:

  1. Once Java updates, the mini version number will change, which makes the static path no longer available.
  2. After changing the JRE used in my system with update-alternatives, I have to modify $JAVA_HOME in .bash_profile. That extra work is annoying.
  3. Some $JAVA_HOME-required programs, is not aware of .bash_profile at all. I have to set $JAVA_HOME in their start-up script, therefore a standard-and-easy way to get $JAVA_HOME seems critical to me.

Well, any advice is welcomed. Thanks.

share|improve this question

5 Answers 5

up vote 4 down vote accepted

A little hacky, but put this line in your /etc/profile for setting it system-wide:

export JAVA_HOME=$(dirname $(dirname $(readlink -e /usr/bin/javac)))
share|improve this answer
Thanks, it works for me. – Jianwen W. Jun 11 '12 at 12:12
Some sysadmins like to fiddle with the PATH so the /usr/bin isn't necessarily the first place looked. To be sure you are picking up the path of the version you will actually run, a better re-hack may be: export JAVA_HOME=$(dirname $(dirname $(readlink -e $(which javac)))) – SimonB Feb 4 at 11:40
this works if the path exists. Is there any way to make it silent on systems that don't have java? – greggles Feb 10 at 13:26

You can set environment variables in /etc/environment, which gets setup by PAM. There should also be symlinks in /usr/lib/jvm which don't change with minor releases.

share|improve this answer

Set path system profile

Edit the system Path file /etc/profile

sudo gedit /etc/profile

Add following lines in end

export JAVA_HOME
export JRE_HOME
export PATH

For for full installation guide.

share|improve this answer

The accepted solution byuser13742 assumes that the server definitely has Java installed. In case you're adding this to a centralized script for many servers it will give some errors like:

dirname: missing operand
Try 'dirname --help' for more information.

To avoid that and only set JAVA_HOME if the server has a Java environment, a slight enhancement is:

if [ -f "$(which javac)" ]; then
    export JAVA_HOME=$(dirname $(dirname $(readlink -e $(which javac))))

I also incorporated the change from SimonB to use which to find where the installation is.

share|improve this answer

If you're sure the 'java' command can be called, wouldn't this work for you?

export JAVA_HOME=$(dirname $(java -XshowSettings:properties -version 2>&1 | grep 'java\.home' | awk '{ print $3 }'))
share|improve this answer
The OP is looking for a way to set $JAVA_HOME system-wide, not just in the current shell. – Celada Feb 24 at 21:12

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.