Make $JAVA_HOME easily changable in Ubuntu [closed]
Asked Answered
T

8

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

Toombs answered 16/4, 2010 at 19:31 Comment(2)
What's the correct site and the duplicate answer?Sheffield
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.Garald
S
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.

Sheffield answered 13/5, 2011 at 15:15 Comment(11)
A restart is still needed for these changes to affect apps launched through the Ubuntu UI (i.e. double clicking an app)Ingleside
How is this different than just setting the variable in every friggin' shell?Inenarrable
@Inenarrable 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.Sheffield
another way to not have to run this manually every time is to modify your .bashrc file to include the command "source /etc/environment"Doubledecker
this did not work for me until i export JAVA_HOME;Keewatin
How do I set an envvar to /etc/environment via the command line, like Windows' setx?Mingrelian
This superuser question suggests a logout/login is needed for this to take effect for all users: superuser.com/questions/339617/…Upcast
@MartinKonicek your example is incorrect. there should be no 'export' on that line. /etc/environment contains key value pairsHandbarrow
@Keewatin see my last commentHandbarrow
keep in mind that /etc/environment is special and does not allow variable substitution, e.g. JRE_HOME=${JAVA_HOME}/jre won't work.Consumerism
@MartinKonicek I don't understand the reason behind it. Configuration files are not executed from what the FHS states.Ciliate
H
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.

Hurried answered 16/4, 2010 at 19:38 Comment(5)
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. "Hen
~/.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.Gran
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.Gabar
@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.Cowles
Yes, I agree with @AlainO'Dea. If there is any error in your script, you will not be able to login. Take care.Leonelleonelle
G
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

Gibbon answered 7/11, 2010 at 8:29 Comment(0)
S
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.

Saccharify answered 7/11, 2010 at 18:0 Comment(0)
S
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.

Salzburg answered 26/2, 2013 at 15:10 Comment(0)
C
1

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

Camacho answered 16/4, 2010 at 19:35 Comment(0)
F
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

Fluxmeter answered 4/4, 2012 at 4:9 Comment(0)
M
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.

Mot answered 18/3, 2013 at 3:32 Comment(1)
You should use ~/.profile rather than ./.profile.Freeswimming

© 2022 - 2024 — McMap. All rights reserved.