Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Can';t使用getruntime.exec()成功执行Shellscript_Java_Shell - Fatal编程技术网

Java Can';t使用getruntime.exec()成功执行Shellscript

Java Can';t使用getruntime.exec()成功执行Shellscript,java,shell,Java,Shell,我已经编写了一段代码来执行来自java的脚本: String wrapper_script=homedir+"/blast_distribute.sh "+" --seqs="+seqs+" --i="+formobj.getUpFile().getFileName()+" "+formobj.getSelected_program(); script_exec=Runtime.getRuntime().exec(wrapper_script); 这对我来说非常适合,因为命令被成功执行

我已经编写了一段代码来执行来自java的脚本:

 String wrapper_script=homedir+"/blast_distribute.sh "+" --seqs="+seqs+" --i="+formobj.getUpFile().getFileName()+"  "+formobj.getSelected_program();

 script_exec=Runtime.getRuntime().exec(wrapper_script);
这对我来说非常适合,因为命令被成功执行。现在我需要以其他用户的身份运行此命令,因此我需要以如下格式执行命令:

su用户名-c“命令”

因此,我将上面的脚本_exec字符串编辑为:

String wrapper_script1="su - "+username+" -c "+"'"+wrapper_script+"'";
我已经打印了脚本1,其中显示:

su-abhijeet-c'/home/abhijeet//blast_distribute.sh--seqs=1562 --i=mPS_0.contigs.fasta'

如果我直接在Linux上运行相同的命令,它将完全按照我的需要工作。但当我运行时:

script\u exec=Runtime.getRuntime().exec(包装器\u script1)
它不能正常工作,在我的错误流中,我得到的错误是

su:无法识别的选项'--seqs=1562'有关详细信息,请尝试'su--help' 信息


我已经尝试了很多次,但无法解决此问题。出现此问题的原因是什么?

您应该为此运行多个参数:

Runtime.getRuntime().exec(new String[] {"su", "-", username, "-c",
        homedir + "/blast_distribute.sh " + " --seqs=" + seqs + " --i=" + formobj.getUpFile().getFileName() + "  " + formobj.getSelected_program()
});
您将得到错误,因为这里不将
视为封闭字符,而是将其视为
su
命令的参数

让我们看看细节。当您在控制台中键入一些命令,如
su-jsmith-c'aba--cabga'
,您所做的是:“使用这样的参数运行命令
su
jsmith
-c
aba--caba
(一个参数用于多个单词)”


在Java代码中也应该做同样的事情。JavaAPI中有一个特殊的命令,用于使用具体参数运行具体命令,而不是像shell do那样解析行::第一个参数是command,下一个是参数。这就是我们在这个代码块中所做的。

感谢您的回复,在su username-c'command'中,command被引用,我需要将引用的command传递给它。您应该尝试一下。您所谓的“quoted”只是一个给shell的信号,即这组字符应被视为一个参数。@AbhijeetPanwar then 127是
su
命令的返回值。您应该查看输出。当我运行
mansu
并查找
127
输出代码时,我看到这一行“请求的命令无法执行”。可能您的脚本不可执行?我没有得到所需的输出,在我的错误流中我得到了-bash:/home/abhijeet//blast\u distribute.sh--seqs=1562--I=mPS\u 0.contigs.fasta:没有这样的文件或目录,但文件和目录存在,它使用了我的第一个语法。@AbhijeetPanwar oops,我忘了
-c
标志,请参阅更新的答案。