以下代码:
ProcessBuilder pb = new ProcessBuilder(new String[] {"echo", "some text", ">", "test"});
不断返回“some text > test
”。
我做错了什么?
修改
这很有用
ProcessBuilder pb = new ProcessBuilder(new String[] {"bash", "-c", "echo sometext > test"});
答案 0 :(得分:3)
尝试以下
ProcessBuilder pb =
new ProcessBuilder("cmd.exe", "/c" ,"echo", "some text", ">", "test");
这适用于 windows
实际上'Echo'不是一个命令,它是windows中的shell(cmd.exe)的内部命令,而 linux 或 unix 中的“bash”。所以,对于Unix / Linux
ProcessBuilder pb =
new ProcessBuilder("bash", "-c","echo", "some text", ">", "test");