Pages

Thursday, November 24, 2011

To Set Global Environment Variables(JAVA_HOME) in CentOS

The easiest way to set an environment variable in CentOS is to use export as in

    # export JAVA_HOME=/usr/java/jdk.1.5.0_12

    # export PATH=$PATH:$JAVA_HOME

However, variables set in such a manner are transient i.e. they will disappear the moment you exit the shell. Obviously this is not helpful when setting environment variables that need to persist even when the system reboots.

In such cases, you need to set the variables within the system wide profile. In CentOS (I’m using v5.3), the folder /etc/profile.d/ is the recommended place to add customizations to the system profile.

For example, when installing the Sun JDK, you might need to set the JAVA_HOME and JRE_HOME environment variables. In this case:

   1. Create a new file called java.sh

          vim /etc/profile.d/java.sh

   2. Within this file, initialize the necessary environment variables

          export JRE_HOME=/usr/java/jdk1.5.0_12/jre
          export PATH=$PATH:$JRE_HOME/bin

          export JAVA_HOME=/usr/java/jdk1.5.0_12
          export JAVA_PATH=$JAVA_HOME

          export PATH=$PATH:$JAVA_HOME/bin

Now when you restart your machine, the environment variables within java.sh will be automatically initialized (checkout /etc/profile if you are curious how the files in /etc/profile.d/ are loaded) .

PS: If you want to load the environment variables within java.sh without having to restart the machine, you can use the source command as in:

   # source java.sh

No comments:

Post a Comment