0

I have a RHEL 7 server. Installed java version is jdk1.8.0_151

# ls -l /usr/java
total 0
drwxr-xr-x. 8 root root 255 Apr 24 14:10 jdk1.8.0_151

I set JAVA_HOME location and added bin directory to PATH variable in /etc/profile

# grep JAVA_HOME /etc/profile
export JAVA_HOME="/usr/java/jdk1.8.0_151/"
export PATH=$JAVA_HOME/bin:$PATH

Still java -version shows false information and echo $JAVA_HOME shows no output.

# java -version
openjdk version "1.8.0_242"
OpenJDK Runtime Environment (build 1.8.0_242-b08)
OpenJDK 64-Bit Server VM (build 25.242-b08, mixed mode)
# 
# echo $JAVA_HOME

#

How can I resolve this?

Update

When executed the following commands java -version and echo $JAVA_HOME works for the user igwuser, but doesn't work when logged in as root.

export PATH=/usr/java/jdk1.8.0_151/bin:$PATH
export JAVA_HOME="/usr/java/jdk1.8.0_151/"

When logged in as root,

[igwuser@dep4 ~]$ sudo su
[root@dep4 igwuser]# java -version
bash: java: command not found...
[root@dep4 igwuser]# echo $JAVA_HOME

[root@dep4 igwuser]#

Why it doesn't work when logged in as root?

Update 2

PATH variable resets when I log in and logout. That should be the issue.

[root@dep4 igwuser]# echo $PATH
/sbin:/bin:/usr/sbin:/usr/bin
[root@dep4 igwuser]# 
[root@dep4 igwuser]# 
[root@dep4 igwuser]# export PATH=/usr/java/jdk1.8.0_151/bin:$PATH
[root@dep4 igwuser]# echo $PATH
/usr/java/jdk1.8.0_151/bin:/sbin:/bin:/usr/sbin:/usr/bin
[root@dep4 igwuser]# 
[root@dep4 igwuser]# exit
exit
[igwuser@dep4 ~]$ sudo su
[root@dep4 igwuser]# echo $PATH
/sbin:/bin:/usr/sbin:/usr/bin
[root@dep4 igwuser]#
  • 1
    Did you log out and log in again after the changes? – Hauke Laging Apr 27 '20 at 10:24
  • What are the outputs of which java (and type java if you use bash)? Did you check that there is actually a java executable in /usr/java/jdk1.8.0_151/bin? – xenoid Apr 27 '20 at 10:26
  • @HaukeLaging: Yes – Pradeep Sanjeewa Apr 27 '20 at 10:48
  • @xenoid: which java = /bin/java , type java = java is /bin/java , /usr/java/jdk1.8.0_151/bin/java -version = java version "1.8.0_151" Java(TM) SE Runtime Environment (build 1.8.0_151-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode) – Pradeep Sanjeewa Apr 27 '20 at 10:59
  • What is the output of echo $PATH? – Hauke Laging Apr 27 '20 at 11:02
1

I was editing the wrong file(/etc/profile) all that time. Adding following lines to /etc/bashrc resolved the issue.

export JAVA_HOME="/usr/java/jdk1.8.0_172-amd64/"
export PATH=$JAVA_HOME/bin:$PATH

Thanks

Update : Please refer the comment by @jsbillings below. creating a custom script in /etc/profile.d/ and adding above lines is the most recommended.

  • 1
    You really should be putting a .sh file in /etc/profile.d/ rather than editing /etc/bashrc. – jsbillings Apr 27 '20 at 23:18
  • @jsbillings: Thanks for the comment. Why is editing /etc/bashrc not recommended? – Pradeep Sanjeewa Apr 28 '20 at 2:26
  • 1
    You want the OS to be able to update that file, since it is owned by a package. Modifying it will prevent the “bash” package from ever updating that file. – jsbillings Apr 28 '20 at 17:40
  • @jsbillings : stackoverflow.com/questions/61568921/… – Pradeep Sanjeewa May 3 '20 at 2:33
0
  1. Each user has his own variable environment. Thus, it's normal that values of PATH and JAVA_HOME variables are different from one user (root) to another one (non-root user).

  2. A persistent way to save environment variables :

    • Add your environment variable by editing ~/.bashrc file : vim ~/.bashrc

    • Load the changes you did in ~/.bashrc using : source ~/.bashrc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.