Sunday, June 20, 2010

Set Environment variables - PATH, CLASSPATH, JAVA_HOME, ANT_HOME in Ubuntu

Setting Environment variables in Ubuntu can be tricky. It comes with OpenOffice which requires OpenJDK, so the path for java is set to that of OpenJDK. The command

$ java -version

works but uses the OpenJDK. Once you have installed Sun JDK the location of the JDK should be "/usr/lib/jvm/java-6-sun-1.6.0.20" depending upon the version you have installed. Once you have installed it you may want to update the PATH, CLASSPATH variables and create environment variables like JAVA_HOME, ANT_HOME, etc.

The location to set up the environment variables in Ubuntu is /home/Your_User_Name/.bashrc. You will need to make entries like:

JAVA_HOME=/usr/lib/jvm/java-6-sun-1.6.0.20
ANT_HOME=/home/harkiran/javaTools/apache-ant-1.8.1
PATH=$PATH:$ORACLE_HOME/bin:$JAVA_HOME/bin:$ANT_HOME/bin
CLASSPATH=.:/usr/lib/jvm/java-6-sun-1.6.0.20/lib
export JAVA_HOME
export ANT_HOME
export CLASSPATH
export PATH

The separator to use in Linux between PATH is ":" (colon). Windows uses ";" (semi colon).

Once you have set this up you will also need to create symbolic links in the "/etc/alternatives" directory. You will need administrative privileges to do so.

$ ln -s /usr/lib/jvm/java-6-sun-1.6.0.20/bin/java /etc/alternatives/java
$ ln -s /usr/lib/jvm/java-6-sun-1.6.0.20/bin/javac /etc/alternatives/javac

Once done you can check using the commands java -version, javac -version, ant to check all is working and has been set up properly.