Set JAVA_HOME/PATH environment variable on CentOS/RHEL

Scenario: Set JAVA_HOME/PATH environment variable on CentOS/RHEL
System: CentOS/RHEL 6/7

– Set JAVA_HOME / PATH for a single user,example set up on user bachem
1) Check java path
Refer to below output the path is /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64/jre

[root@centos7ht ~]# java -version
openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)
[root@centos7ht ~]#
[root@centos7ht ~]# alternatives --config java

There is 1 program that provides 'java'.

  Selection    Command
-----------------------------------------------
*+ 1           java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64/jre/bin/java)

Enter to keep the current selection[+], or type selection number: 1
[root@centos7ht ~]#

2) Login as bachem then edit .bash_profile
# su - bachem
$ vi /home/bachem/.bash_profile

add below

export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64/jre
PATH=$PATH:$HOME/.local/bin:$HOME/bin:$JAVA_HOME/bin

3) Apply Java environment and path, run below command
$ source /home/bachem/.bash_profile

Output:

[bachem@centos7ht ~]$ echo $JAVA_HOME

[bachem@centos7ht ~]$ echo $PATH
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/bachem/.local/bin:/home/bachem/bin
[bachem@centos7ht ~]$ source /home/bachem/.bash_profile
[bachem@centos7ht ~]$ echo $JAVA_HOME
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64/jre
[bachem@centos7ht ~]$ echo $PATH
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/bachem/.local/bin:/home/bachem/bin:/home/bachem/.local/bin:/home/bachem/bin:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64/bin
[bachem@centos7ht ~]$

– Set JAVA_HOME / PATH for a all user
1) Edit /etc/profile, example using java path above.
# vi /etc/profile
add below

export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64/jre
PATH=$PATH:$HOME/.local/bin:$HOME/bin:$JAVA_HOME/bin

2) Apply Java environment and path, run below command
$ source /etc/profile

Output:

[root@centos7ht ~]# echo $JAVA_HOME

[root@centos7ht ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@centos7ht ~]# vi /etc/profile
[root@centos7ht ~]# source /etc/profile
[root@centos7ht ~]# echo $JAVA_HOME
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64/jre
[root@centos7ht ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/root/.local/bin:/root/bin:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64/jre/bin
[root@centos7ht ~]# su - user2
Last login: Fri Oct  5 00:53:06 WIB 2018 on pts/0
[user2@centos7ht ~]$ echo $JAVA_HOME
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64/jre
[user2@centos7ht ~]$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/sempak/.local/bin:/home/sempak/bin:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64/jre/bin:/home/sempak/.local/bin:/home/sempak/bin
[user2@centos7ht ~]$

References:
https://www.cyberciti.biz/faq/linux-unix-set-java_home-path-variable/
http://sharadchhetri.com/2013/06/03/how-to-set-java-environment-variables-in-linux-or-centos/


Leave a Reply

Your email address will not be published. Required fields are marked *