Sunday, February 04, 2007

Setting path variables in Linux permanently

Recently I have installed Linux Ubuntu on my laptop on top of Virtual PC. There is a very good post on how to do this at this link:
http://arcanecode.wordpress.com/2006/12/19/installing-ubuntu-on-virtualpc-step-by-step/

Then I had installed Eclipse and JAVA 1.5 on the new linux workstation! I wanted to work out a few programs on the linux platform.

When you start the terminal in Ubuntu and type "java -version", it displays the version but as you notice down it is not the Sun HotSpot VM that is running but is the GCJ, a free java compiler which doesnt actually interpret. So you would like to change the system to use the Java VM you installed. For that the obvious way is to change the Path variables for JAVA_HOME and PATH.

You can set a path variable using "export" command as :

export JAVA_HOME=/opt/jdk1.5.0_11
export PATH=$JAVA_HOME/bin:$PATH

When we set PATH variable, we should not forget to append already existing path variables( using :$PATH).

Now when you type "java -version", you can see your Java VM as HotSpot one. But then close the terminal and open it again.

Again try the "java -version", you will find your settings to be gone! What happens is that your settings were temporary and effective only for the terminal window that was launched and path set. In order for the variables to be effective for the user throughout the sessions, you should edit the bashrc script.

You can set environment variables permanently for a given user by changing the bashrc:

>pico ~/.bashrc

This opens the bashrc for you. Paste the two lines of "export" i mentioned earlier. Make sure you give the path of your JDK properly.
Save the changes and exit.

Now type

>source ~/.bashrc

This updates the changes made the new variables have been set. To test your settings you can now type:
"which java" and it should display the path of the java compiler that is being use( the one you set in the bashrc).

I am trying to find out how to set variables to reflect on all the user accounts !!! Let me know in case you guys know how to!

1 comment:

Anonymous said...

Thanks man.
You solved a problem I had.