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

标签 java linux windows

如何在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/

相关文章:

c - 消息队列回调

c# - 在 Windows 10 中部署 Windows Phone 8.1 应用程序时找不到 IpOverUsbSvc

C - 无法绑定(bind)服务器套接字

java - 如何将音频文件转换为字节数组

java - setSession() 没有被调用?

java - 从事务测试中查看数据 - Hibernate + Spring + MySQL

c++ - 如何在 Win x64 上使用 WinAPI 正确安装虚拟打印机?

java - ThreadLocal init 上的 FindBugs 警告

c - 编译器如何以及为什么将 extern 函数替换为空函数?

php - 将 Linux Curl 转换为 PHP