linux sudo java : command not found

Open source software supply chain lighting plan, waiting for you>>>

Running sudo Java – version reports an error

[ admin@localhost xxx]$ sudo java -version
sudo: java: command not found

Reason:

When using sudo to execute a program, for security reasons, the program will be executed in a new and minimized environment. Environment variables such as path have been reset to the default state under the sudo command. So when a newly initialized path variable does not contain the directory of the program you want to run, and you use sudo to execute it, you will get an error prompt of “command not found”

View the path variable under the sudo command

sudo printenv PATH
/sbin:/bin:/usr/sbin:/usr/bin

Solution:

Set the path used by sudo command to the path of the current shell (recommended)

$ sudo env PATH=$PATH java -version

Alternatively, let the subshell use the path of the current environment (recommended)

$ sudo PATH=$PATH sh -c "java -version"

Or, use the visudo command to modify the configuration of the sudo command for secure path (not recommended)

$ sudo visudo

23558; ect/sudoersDefaults secure path =/sbin:/bin:/usr/sbin:/usr/bin

Defaults secure path =/sbin:/bin:/usr/sbin:/usr/bin:/YourJdkHome/bin

Similar Posts: