JAVA_HOME not found as Sudo

前端 未结 3 733
傲寒
傲寒 2021-01-01 21:22

I have a bash script on a Linux box that runs a Jar file. When logged in as a regular user I don\'t have permission to run the script, but it prints the following log:

相关标签:
3条回答
  • 2021-01-01 21:47

    "sudo -E " didn't solve the problem when JAVA_HOME was not exported. And when it was exported, "sudo " without -E works the same.

    So you can add export JAVA_HOME=.../jdk<version> in your .bash_profile and .bashrc file.

    In case you wondered what's the difference of .bash_profile and .bashrc, .bash_profile is executed upon login (e.g., show some diagnostic/welcome information). .bash_rc is executed when you open a new terminal (e.g., shift-ctrl-T).

    In order to run some commands for both cases, you can put it in .bashrc file, and let .bash_profile source .bashrc:

    if [ -f ~/.bashrc ]; then
       source ~/.bashrc
    fi
    
    0 讨论(0)
  • 2021-01-01 21:55

    By default, sudo will cleanup the environment of the spawned commands. Pass -E to keep it:

    sudo -E env
    

    Compare to:

    sudo env
    
    0 讨论(0)
  • 2021-01-01 21:59

    You could always just pass it to java explicitly like this:

    sudo java -Djava.home=$JAVA_HOME Test

    0 讨论(0)
提交回复
热议问题