JAVA_HOME没有找到Sudo

时间:2021-04-29 02:41:47

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:

我在运行Jar文件的Linux机器上有一个bash脚本。作为普通用户登录时,我没有运行脚本的权限,但它会打印以下日志:

*INFO * Using JVM found at /opt/jdk6/bin/java

When I try to use the script with Sudo though, it gives:

当我尝试使用Sudo脚本时,它给出了:

*ERROR* Unable to locate java, please make sure java is installed and JAVA_HOME set

I've set JAVA_HOME to the same path above — can see it with echo $JAVA_HOME & it's also set as an option within the script. I'm happy that the script isn't the issue — it's a default CQ5 control script & I'm using it on dozens of other boxes without issue. Just unsure what I'm doing wrong above & presume it's something I'm missing re Linux set-up?

我已经将JAVA_HOME设置为上面的相同路径——可以使用echo $JAVA_HOME查看它,它也被设置为脚本中的一个选项。我很高兴这个脚本不是问题所在——它是一个默认的CQ5控制脚本,我可以在其他几十个没有问题的盒子上使用它。只是不确定我在上面做错了什么&假设这是我错过的Linux设置?

When I run the sudo command, does it have access to the JAVA_HOME that I set up as myself?

当我运行sudo命令时,它是否可以访问我设置为自己的JAVA_HOME ?

3 个解决方案

#1


24  

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

默认情况下,sudo将清理衍生命令的环境。Pass -E保存:

sudo -E env

Compare to:

比较:

sudo env

#2


1  

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

当JAVA_HOME没有被导出时,“sudo -E”并没有解决这个问题。当它被导出时,“sudo”没有-E也一样。

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

因此可以添加export JAVA_HOME=…/jdk <在.bash_profile和.bashrc文件中版本> 。

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).

如果您想知道.bash_profile和.bashrc的区别,.bash_profile将在登录时执行(例如,显示一些诊断/欢迎信息)。

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

为了对这两种情况运行一些命令,您可以将其放入.bashrc文件中,并让.bash_profile source .bashrc:

if [ -f ~/.bashrc ]; then
   source ~/.bashrc
fi

#3


0  

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

你可以像这样明确地把它传递给java:

sudo java -Djava.home=$JAVA_HOME Test

java -Djava sudo。家= $ JAVA_HOME测试

#1


24  

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

默认情况下,sudo将清理衍生命令的环境。Pass -E保存:

sudo -E env

Compare to:

比较:

sudo env

#2


1  

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

当JAVA_HOME没有被导出时,“sudo -E”并没有解决这个问题。当它被导出时,“sudo”没有-E也一样。

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

因此可以添加export JAVA_HOME=…/jdk <在.bash_profile和.bashrc文件中版本> 。

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).

如果您想知道.bash_profile和.bashrc的区别,.bash_profile将在登录时执行(例如,显示一些诊断/欢迎信息)。

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

为了对这两种情况运行一些命令,您可以将其放入.bashrc文件中,并让.bash_profile source .bashrc:

if [ -f ~/.bashrc ]; then
   source ~/.bashrc
fi

#3


0  

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

你可以像这样明确地把它传递给java:

sudo java -Djava.home=$JAVA_HOME Test

java -Djava sudo。家= $ JAVA_HOME测试