Presentation

Let’s suppose you need to assign “/usr/lib/jvm/java-1.8.0” to the JAVA_HOME environment variable.

You can need to set up an environment variable in at least three different cases:

  • for a simple user (we will take the centos user as an exemple),
  • for the root user,
  • for all the users (root included).

Simple User Configuration

Connect as the centos user (or any user that you chose except root). By default you should be at the root of the home directory. If it’s not the case, type cd.

Edit the .bash_profile file and paste the following line at the end of the file:

export JAVA_HOME=”/usr/lib/jvm/java-1.8.0″

Root User Configuration

Connect as the rootuser. By default you should be under /root. If it’s not the case, type cd.

Edit the .bash_profile file and paste the following line at the end of the file:

export JAVA_HOME=”/usr/lib/jvm/java-1.8.0″

All Users Configuration

Connect as the rootuser. Create a file with any name finishing by .sh in the /etc/profile.d directory (here we will use javaenv.sh).

Paste the following line into the file:

export JAVA_HOME=”/usr/lib/jvm/java-1.8.0″

Set the correct access rights on the file:

# chmod 0755 /etc/profile.d/javaenv.sh

Note: This is required otherwise only the root user would get the environment variable assigned.

Source: StackExchange.com