2

Windows 7cmd执行没有问题ping -n 5 127.0.0.1 > nul。此外,Runtime.getRuntime.exec(new String[]{"ping", "-n", "5", "127.0.0.1"})工作正常。

但是Runtime.getRuntime.exec(new String[]{"ping", "-n", "5", "127.0.0.1", ">", "nul"})失败了Bad parameter >。为什么?

我在 Java6 模式下使用 Java7。

4

2 回答 2

2

>重定向不是 ping 命令的一部分,它是其cmd自身的一部分。当exec()看到>它时,它会尝试将其ping作为参数提供给它。

InputStream要获得相同的功能,只需从Process exec返回值中读取(并忽略)数据。

于 2013-04-18T08:01:04.853 回答
1

Because > is not a valid argument for ping. When executed on the command prompt the > is interpreted as output direction, but when used from Runtime().exec() it is not interpreted and is passed to ping as an argument (hence the error message).

To capture the output (unintuitively) use Process.getInputStream() (an instance of Process is returned by Runtime.exec()).

于 2013-04-18T07:57:09.170 回答