138

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)?

Michael Scheper
  • 6,514
  • 7
  • 63
  • 76
Junho Park
  • 1,491
  • 2
  • 10
  • 11
  • 29
    What's the correct site and the duplicate answer? – Martin Konicek Sep 03 '12 at 15:45
  • The `~/.profile` file is only read when you login to Ubuntu, so if you logout/login then JAVA_HOME is set for all terminals and any other applications you run. If you set JAVA_HOME in `~/.bashrc` it will only be seen by applications run from the terminal. – practicalli-john Jan 21 '15 at 13:52

8 Answers8

197

Put the environment variables into the global /etc/environment file:

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

Execute "source /etc/environment" in every shell where you want the variables to be updated:

$ source /etc/environment

Check that it works:

$ echo $JAVA_HOME
$ /usr/lib/jvm/java-1.5.0-sun

Great, no logout needed.

If you want to set JAVA_HOME environment variable in only the terminal, set it in ~/.bashrc file.

Martin Konicek
  • 39,126
  • 20
  • 90
  • 98
  • 10
    A restart is still needed for these changes to affect apps launched through the Ubuntu UI (i.e. double clicking an app) – Eric Hu Feb 12 '13 at 03:15
  • 7
    How is this different than just setting the variable in every friggin' shell? – mlissner Aug 22 '13 at 18:48
  • @mlissner Yes you need to run a command in each shell, but the value also stays in the `/etc/environment` file permanently. When just setting the variable in every shell, it would be lost again after restart. – Martin Konicek Aug 22 '13 at 20:45
  • 1
    another way to not have to run this manually every time is to modify your .bashrc file to include the command "source /etc/environment" – nityan Jan 08 '14 at 01:54
  • this did not work for me until i `export JAVA_HOME;` – psychok7 Apr 30 '14 at 15:44
  • How do I set an envvar to /etc/environment via the command line, like Windows' setx? – Neil McGuigan Jul 23 '14 at 23:03
  • This superuser question suggests a logout/login is needed for this to take effect for all users: http://superuser.com/questions/339617/how-to-reload-etc-environment-without-rebooting – Greg K Dec 03 '14 at 13:45
  • 1
    @MartinKonicek your example is incorrect. there should be no 'export' on that line. /etc/environment contains key value pairs – pdeva Mar 21 '15 at 08:04
  • @psychok7 see my last comment – pdeva Mar 21 '15 at 08:04
  • 1
    keep in mind that [`/etc/environment` is special](http://askubuntu.com/a/149668/132098) and does not allow variable substitution, e.g. `JRE_HOME=${JAVA_HOME}/jre` won't work. – Abdull Nov 27 '15 at 09:44
  • @MartinKonicek I don't understand the reason behind it. Configuration files are not executed from what the FHS states. – Abhishek Bhatia Feb 01 '16 at 23:32
31

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
  • 1,695
  • 18
  • 29
  • From the link you provided: "The below are not recommended but the previous solution (?) provided on this page did not work with the 10.04 desktop release. .pam_environment is not naturally procesed with the distribution. " – Calmarius Oct 05 '11 at 07:51
  • 14
    ~/.pam_environment is an awesome way to brick your login if you assume it processes variables. I tried setting PATH to ${PATH}:${HOME}/bin and it failed to evaluate the variables. Net result: cannot log in :( Fixed by visiting Ctrl-Alt-1 and running "/bin/rm ~/.pam_environment". Be exceptionally careful or use a secondary account to test settings here. – Alain O'Dea Apr 04 '12 at 03:34
  • 6
    Personally I don't know why they bothered creating the environment file and then had it work in a completely different way than any other mechanism for setting environment variables ever worked before. Simple key value pairs like the environment file uses is plain BS. There is a reason we use variables and links in the environment and it still has merit today. It simplifies administration. Whoever the bozos are who decided the PAM way of doing things in this file was best are imbeciles at best. Variables NEED to be interpreted. – Bill Rosmus Jul 26 '12 at 18:59
  • 1
    @AlainO'Dea: I had exactly the same issue. Shocked and worried when I couldn't get past login screen. After some digging around I found that you can use variables but have to use a different syntax. eg. PATH DEFAULT=${PATH}:${HOME}/bin When it's not using a variable, a simple assignment seems to work. – tim_wonil Jul 27 '12 at 03:36
  • Yes, I agree with @AlainO'Dea. If there is any error in your script, you will not be able to login. Take care. – Evan Hu Feb 11 '15 at 01:56
13

Try these steps.

--We are going to edit "etc\profile". The environment variables are to be input at the bottom of the file. Since Ubuntu does not give access to root folder, we will have to use a few commands in the terminal

Step1: Start Terminal. Type in command: gksudo gedit /etc/profile

Step2: The profile text file will open. Enter the environment variables at the bottom of the page........... Eg: export JAVA_HOME=/home/alex/jdk1.6.0_22/bin/java

export PATH=/home/alex/jdk1.6.0_22/bin:$PATH

step3: save and close the file. Check if the environment variables are set by using echo command........ Eg echo $PATH

prayagupa
  • 30,204
  • 14
  • 155
  • 192
Alex
  • 1,618
  • 1
  • 17
  • 25
9

You need to put variable definition in the ~/.bashrc file.

From bash man page:

When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if these files exist.

Paweł Nadolski
  • 8,296
  • 2
  • 42
  • 32
5

Traditionally, if you only want to change the variable in your terminal windows, set it in .bashrc file, which is sourced each time a new terminal is opened. .profile file is not sourced each time you open a new terminal.

See the difference between .profile and .bashrc in question: What's the difference between .bashrc, .bash_profile, and .environment?

.bashrc should solve your problem. However, it is not the proper solution since you are using Ubuntu. See the relevant Ubuntu help page "Session-wide environment variables". Thus, no wonder that .profile does not work for you. I use Ubuntu 12.04 and xfce. I set up my .profile and it is simply not taking effect even if I log out and in. Similar experience here. So you may have to use .pam_environment file and totally forget about .profile, and .bashrc. And NOTE that .pam_environment is not a script file.

Community
  • 1
  • 1
HongboZhu
  • 4,442
  • 3
  • 27
  • 33
1

I know this is a long cold question, but it comes up every time there is a new or recent major Java release. Now this would easily apply to 6 and 7 swapping.

I have done this in the past with update-java-alternatives: http://manpages.ubuntu.com/manpages/hardy/man8/update-java-alternatives.8.html

Alain O'Dea
  • 21,033
  • 1
  • 58
  • 84
1

After making changes to .profile, you need to execute the file, in order for the changes to take effect.

root@masternode# . ~/.profile

Once this is done, the echo command will work.

CodeShane
  • 6,480
  • 1
  • 18
  • 24
1

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

Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171