views:

743

answers:

4

In Ubuntu, I'd like to switch my JAVA_HOME environment variable back and forth between Java 5 and 6.

I open a terminal and type in the following to set the JAVA_HOME environment variable:

export JAVA_HOME=/usr/lib/jvm/java-1.5.0-sun

And in that same terminal window, I type the following to check that the environment variable has been updated:

echo $JAVA_HOME

And I see /usr/lib/jvm/java-1.5.0-sun which is what I'm expecting to see. In addition, I modify ~/.profile and set the JAVA_HOME environment variable to /usr/lib/jvm/java-1.5.0-sun.

And now for the problem--when I open a new terminal window and I check my JAVA_HOME environment variable by typing in echo $JAVA_HOME I see that my JAVA_HOME environment variable has been reverted back to Java 6. When I reboot my machine (or log out and back in, I suppose) the JAVA_HOME environment variable is set to Java 5 (presumably because of the modification I made in my ~/.profile).

Is there a way around this so that I can change my JAVA_HOME environment without having to log out and back in (AND make that environment variable change stick in all new terminal windows)?

A: 

Take a look at bash(1), you need a login shell to pickup the ~/.profile, i.e. the -l option.

Nikolai N Fetissov
+1  A: 

This will probably solve your problem: https://help.ubuntu.com/community/EnvironmentVariables

Session-wide environment variables

In order to set environment variables in a way that affects a particular user's environment, one should not place commands to set their values in particular shell script files in the user's home directory, but use:

~/.pam_environment - This file is specifically meant for setting a user's environment. It is not a script file, but rather consists of assignment expressions, one per line.

Not recommended:

~/.profile - This is probably the best file for placing environment variable assignments in, since it gets executed automatically by the DisplayManager during the startup process desktop session as well as by the login shell when one logs-in from the textual console.

Holger Frohloff