1
Ubuntu 11.04
open terminal
JAVA_HOME=/usr/lib/jvm/java-6-sun-1.6.0.24/
export JAVA_HOME
echo $JAVA_HOME 
==>/usr/lib/jvm/java-6-sun-1.6.0.24/

i close the terminal 
open another terminal
echo $JAVA_HOME 
==>

Any idea why JAVA_HOME was not saved ?

Cris
  • 235
  • 1
  • 7
  • 13

2 Answers2

2

The environment variable is only set for that session and its children. When you close that terminal process, those environment variables are gone. Typically I would set them in ~/.profile, but that may not be the preferred method.

The Ubuntu documentation for this is here

micah
  • 974
  • 6
  • 11
1

Your missing the "-p" for "permanent".

It has some bugs, but here is what I use.

# Set the JAVA_HOME variable
function set_java_home {
  echo "Searching for java ..."
  if [ -z $JAVA_HOME ]; then
      echo "Using default value for JAVA_HOME: /usr/java/default"
      JAVA_HOME=/usr/java/default
  fi
  export -p JAVA_HOME
  echo $JAVA_HOME > java.home.config
  sudo rm /etc/alternatives/java
  sudo ln -s $JAVA_HOME/bin/java /etc/alternatives/java
  echo "JAVA_HOME variable set to $JAVA_HOME and /etc/alternatives set."
}
if [ -f java.home.config ]; then
  JAVA_HOME=$(<java.home.config)
else
  JAVA_HOME_CANDIDATES=$(find /usr -type d -name '*jdk1.6*')
  echo "Found the following candidates for JAVA_HOME. Pick one: "
  echo "---"
  echo $JAVA_HOME_CANDIDATES
  echo "---"
  read USER_SUBMITTED_JAVA_HOME
  echo "You chose $USER_SUBMITTED_JAVA_HOME ."
  JAVA_HOME=${USER_SUBMITTED_JAVA_HOME}
fi
set_java_home
djangofan
  • 4,172
  • 10
  • 45
  • 59
  • 1
    Are you referring to "export -p" ? I don't believe that is for permanent. From the man: "if the -p option is given, a list of exported names is displayed. The -p option displays output in a form that may be reused as input." http://www.gnu.org/software/bash/manual/bash.html#Bourne-Shell-Builtins – micah Jun 24 '11 at 21:21