Java 具有多个命令的ProcessBuilder
我想使用ProcessBuilder执行多个命令。我想避免使用脚本文件,在Java中只使用硬编码字符串 这是我要执行的当前文件Java 具有多个命令的ProcessBuilder,java,linux,sh,processbuilder,Java,Linux,Sh,Processbuilder,我想使用ProcessBuilder执行多个命令。我想避免使用脚本文件,在Java中只使用硬编码字符串 这是我要执行的当前文件 #!/bin/sh if [ -e /tmp/pipe ] then rm /tmp/pipe fi mkfifo /tmp/pipe tail -f /dev/null >/tmp/pipe & # keep pipe alive cat /tmp/pipe | omxplayer $1 -r & 现在,这是我当前的代码 private
#!/bin/sh
if [ -e /tmp/pipe ]
then
rm /tmp/pipe
fi
mkfifo /tmp/pipe
tail -f /dev/null >/tmp/pipe & # keep pipe alive
cat /tmp/pipe | omxplayer $1 -r &
现在,这是我当前的代码
private static final String[][] commands = {
{"rm", "-f", "/tmp/airpi_pipe"},
{"mkfifo", "/tmp/airpi_pipe"},
{"tail", "-f", "/dev/null", ">", "/tmp/airpi_pipe"}
};
public static void main(String[] args) throws IOException {
for (String[] str : commands) {
ProcessBuilder pb = new ProcessBuilder(str);
pb.redirectErrorStream(true);
Process process = pb.start();
InputStream is = process.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(is));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
System.err.println("next one");
}
}
显然,tail命令在我的ProcessBuilder中不起作用。我甚至没有试过使用cat/tmp/pipe | omxplayer$1-r&
所以,我的问题是,我如何能够用ProcessBuilder执行我的sh脚本的内容,但只使用硬编码命令而不使用脚本文件,就像我正在尝试的那样?
多谢各位
更新
我必须使用新的ProcessBuilder/bin/sh,-c;让它工作 提示:如何在命令行上执行与单行相同的任务?请记住,必须调用shell才能获得shell功能,否则命令将直接从Java.new ProcessBuilder/bin/sh,-c运行;这个代码很有魅力,谢谢!