0

I installed a JDK in /opt, and placed a shell script in /etc/profile.d/ where I used export to modify the PATH variable so I could run Java nicely. Thing is, this only worked for my local account, not sudo. If I try to sudo the command, it says sudo: java: command not found. I've tried putting the export commands at the end of .bashrc and rc.local, but neither option seems to change this behavior. I want to be able to run sudo java, so where can I modify the PATH at bootup time in such a way that sudo sees it?

Edit: For the record, these are the lines I've added in every place I've attempted:

export JAVA_HOME="/opt/jdk-16.0.2"
export PATH="$PATH:${JAVA_HOME}/bin"
HydroPage
  • 103
  • 3

1 Answers1

0

Your problem is not everyone, or even anyone=root. Your problem is ONLY sudo because sudo uses its own PATH; it does NOT use the one set in your (calling) shell, or any other shell or shell config (unless you use sudo -i which does run the shell of the target-user, default root, with its usual 'login' configuration) because using a user-controllable PATH could allow security to broken, in violation of sudo's entire reason for existence. See

How to set path for sudo commands
Why does `sudo env "PATH=$PATH"` do anything at all?
Passing PATH through sudo
Problems with PATH variable as sudo user

Your most direct solution is to use the full pathname of the program you want to run; rather than typing it out you can do

sudo $(which java) # or maybe $(type -p java) in bash 

which searches java in your shell's PATH then invokes sudo with the result.