0

I'm trying to install eclipse on xubuntu, I installed java with sudo gdebi <java-package>.deb, I added it to the /etc/environment to both PATH and "JAVA_HOME" then reloaded the environment and also rebooted the whole system.

running java --version I got the correct output

java 12.0.1 2019-04-16
Java(TM) SE Runtime Environment (build 12.0.1+12)
Java HotSpot(TM) 64-Bit Server VM (build 12.0.1+12, mixed mode, sharing)

both if called by superuser

$ sudo su
# java --version

or by

$ java --version

but running

$ sudo java --version

I got java: command not found

but running

sudo echo $PATH 

I got

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/jdk-12.0.1/bin/:/snap/bin/

and path to java is present: /usr/lib/jvm/jdk-12.0.1/bin/

How can I fix it? also could someone explain my why this happens?

  • Can you sudo su and provide output echo $PATH in active session. Try to run java by absolute path in active sudo session. – Yurij Goncharuk May 22 at 8:47
  • 3
    sudo echo $PATH would expand $PATH before sudo is called. It would be better to use something like sudo sh -c 'echo "$PATH"' (note the quoting). – Kusalananda May 22 at 8:56
  • That's why I ask @DDS to provide echo in active session. – Yurij Goncharuk May 22 at 9:04
2

This looks like the PATH is being overwritten by sudo.

You should check to see if /etc/sudoers contains a secure_path=... setting and ads your java path to this.


Be wary of simple tests. To confirm your path you should:

sudo bash -c 'echo $PATH'

The simpler form sudo echo $PATH does not work because PATH is expanded before sudo is invoked.

  • See edit: $ sudo echo $PATH, $ echo $PATH and # echo $PATH all are the same and contains java path, only difference is absence of /snap/bin in 'root' call – DDS May 22 at 8:30
  • @DDS edited, still do check your /etc/sudoers for a secure_path. – Philip Couling May 22 at 9:13
  • I did, you are right! I added java path to secure path and it now works! – DDS May 22 at 10:53

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.