The easiest way to set an environment variable in CentOS is to use export as in
<pre>$> export JAVA_HOME=/usr/java/jdk.1.5.0_12
$> export PATH=$PATH:$JAVA_HOME</pre>
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.2), 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:
Create a new file called java.sh
Within this file, initialize the necessary environment variables
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