简体   繁体   English

通过Java中的exec在终端中运行命令而不是以root身份运行

[英]Running commands in a terminal via exec in Java NOT as root

I've been playing around with exec and although it opens a terminal with the user being me, it doesn't seem to be able to execute commands I give it =/ code is as follows: 我一直在玩exec,虽然它以用户身份打开了一个终端,但似乎无法执行我给它的命令= /代码如下:

try{
  Runtime rt = Runtime.getRuntime();
  Process proc = rt.exec("xterm -e \"source /home/USER/.bashrc; ~/./myscript.sh\"")
}
catch(Throwable t)
  t.printStackTrace();

In the xterm console I get: 在xterm控制台中,我得到:

xterm: Can't execvp ": No such file or directory
user@user:$

Which is then a terminal waiting to be used, and will not go away until I ctrl-d it. 然后,这是一个等待使用的终端,直到我按住ctrl-d才会消失。

Not sure what's going on here?... 不知道这是怎么回事?...

Thanks. 谢谢。

I don't think you can pass shell stuff into xterm's -e option. 我认为您不能将shell内容传递给xterm的-e选项。 It wants a program and optional parameters for that program. 它需要一个程序和该程序的可选参数。 source is a shell built-in. source是内置的shell。

Something you can try doing is just calling your myscript.sh and modifying it so that it sources your bashrc at the top. 您可以尝试做的只是调用myscript.sh并对其进行修改,以使其在顶部显示bashrc。 Or create a new bash script that sources your rc file then runs myscript.sh . 或创建一个新的bash脚本来获取rc文件,然后运行myscript.sh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM