如何在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中使用命令“type data.json”,它将像在linux上使用“cat data.json”一样打印json。我很困惑到底发生了什么事?我想在 Windows 和 Linux 上运行命令。如有任何帮助,我们将不胜感激!
谢谢!
最佳答案
尝试使用 ProcessBuilder 重定向进程上的错误流。该命令很可能正在写入 STDErr。您还可以使用 Process.getErrorStream() 获取该流。
关于java - 如何在Windows中使用Runtime.getRuntime().exec(command)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23228593/