2014-04-22 55 views
0

如何在Windows中使用Runtime.getRuntime().exec(command)?如何在Windows中使用Runtime.getRuntime()。exec(command)?

1. 
command = "cat data.json"; // works in linux terminal 
Runtime.getRuntime().exec(command) // runs in linux => Runs OK 

2. 
command = "type data.json"; // works in windows cmd 
Runtime.getRuntime().exec(command) // runs in windows => Fails to run 

3. 
command = "cmd /C type data.json"; // works in windows cmd 
Runtime.getRuntime().exec(command) // runs in windows => Runs OK 
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); // Output is empty 

在第三种情形,从input.readLine()的输出是空的,但如果我在cmd中使用命令式“data.json”,它会打印JSON它会在Linux上以同样的方式与“cat data.json”。我很困惑发生了什么事?我想在Windows和Linux上运行COMMAND。任何帮助表示赞赏!

谢谢!

+0

发布的代码不会显示您分配变量'p'? –

回答

0

尝试使用ProcessBuilder重定向错误流。在所有可能的情况下,命令正在写给STDErr。您也可以使用Process.getErrorStream()来获取该流。

相关问题