Take the 2-minute tour ×
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

3 Answers 3

up vote 2 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

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.

JAVA_HOME=/usr/lib/jvm/java-6-sun
share|improve this answer

Set path system profile

Edit the system Path file /etc/profile

sudo gedit /etc/profile

Add following lines in end

JAVA_HOME=/usr/lib/jvm/jdk1.7.0
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export JRE_HOME
export PATH

For http://javaandme.com/ for full installation guide.

share|improve this answer

Your Answer

 
discard

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.