Tutorial for configuring Java environment variables on CentOS

  • 2020-04-01 03:59:17
  • OfStack

After rhel and centos Linux use yum command, install OpenSDK to /usr/lib-/jvm/directory:


[root@dabu.info ~]# ls -l /usr/lib/jvm/

The output is as follows:


total 4
drwxr-xr-x. 3 root root 4096 Feb 24 17:00 java-1.7.0-openjdk-1.7.0.51.x86_64
lrwxrwxrwx. 1 root root  21 Feb 24 17:00 jre -> /etc/alternatives/jre
lrwxrwxrwx. 1 root root  27 Feb 24 17:00 jre-1.7.0 -> /etc/alternatives/jre_1.7.0
lrwxrwxrwx. 1 root root  38 Feb 24 17:00 jre-1.7.0-openjdk.x86_64 -> java-1.7.0-openjdk-1.7.0.51.x86_64/jre
lrwxrwxrwx. 1 root root  29 Feb 24 17:00 jre-openjdk -> /etc/alternatives/jre_openjdk

Explanation: the d in drwxr-xr-x. in the second line of the output is the folder. The remaining l represents the link.

Set JAVA_HOME to point to a directory containing Java executables (typically in bin/ Java). We use the CD command to go into the only directory under the JVM, java-1.7.0-openjdk-1.7.0.51.x86_64, and the link to jell-1.7.0-openjdk.x86_64 is to   Java-1.7.0-openjdk-1.7.0.51.x86_64 is the folder, so you can point JAVA_HOME directly to the link of jax-1.7.0-openjdk.x86_64 with the export command.
Export command:


[root@dabu.info ~]# export JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.51.x86_64

However, this only works in the current session, and once you log off, it doesn't work.
So save it to a $HOME/.bashrc file (this file is for the specified user, if you are called dabu user, then use dabu user login, and then enter the command: echo $HOME/.bashrc, it will show you the file path you want to modify)
When the file is found, add a line at the bottom of the file as follows:


export JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.51.x86_64

Save the closed file after modification.

Or let all users on the system use Java (openjdk), do the following:


[root@dabu.info ~]# vi /etc/profile

Paste the following three lines into /etc/profile    In:


export JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.51.x86_64
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin

Save closed, after execution:


[root@dabu.info ~]# source /etc/profile

Let the Settings take effect immediately.

Enter the following command to confirm whether the three variables are set as we want:


[root@dabu.info ~]# echo $JAVA_HOME
[root@dabu.info ~]# echo $CLASSPATH
[root@dabu.info ~]# echo $PATH


Related articles: