Linux Logo

Set the Envionrment Variable in CentOS through Best Practice

In the past, to make an environment variable perminant, I would append to the /etc/profile file the environment variable for JAVA_HOME for example and update the PATH. While this does work, it has recently come to my attention that a more appropriate and easier method exists which does not involve editing the /etc/profile file.

For this example, the environment variable, JAVA_HOME will be created for Java. Navigate to the /etc/profile.d directory. Use vim or your favorite editor to create a file called java.sh. With the full path, the file is /etc/profile.d/java.sh.

This Java was installed using yum. Your path may likely vary. Add the following content to the java.sh file and save it. Done.

export JRE_HOME=/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre
export PATH=$PATH:$JRE_HOME/bin

export JAVA_HOME=/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64
export JAVA_PATH=$JAVA_HOME

export PATH=$PATH:$JAVA_HOME/bin

To avoid restarting the server, issue the following command to start the script.

source java.sh

Check your path

# echo $JAVA_HOME
/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64

Source(s)
http://blogs.alliedtechnique.com/2009/04/16/setting-global-environment-variables-in-centos/
http://unix.stackexchange.com/questions/64258/what-do-the-scripts-in-etc-profile-d-do
http://stackoverflow.com/questions/16271316/setting-java-home-classpath-in-centos-6
http://serverfault.com/questions/4995/changes-to-etc-bashrc