Mua*_*ath 6 java networking process runtime.exec windows-7
我正在尝试运行.bat文件并获取输出.我可以运行它,但我无法在Java中获得结果:
String cmd = "cmd /c start C:\\workspace\\temp.bat";
Runtime r = Runtime.getRuntime();
Process pr = r.exec(cmd);
BufferedReader stdInput = new BufferedReader(
new InputStreamReader( pr.getInputStream() ));
String s ;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
Run Code Online (Sandbox Code Playgroud)
结果是null
.不知道我为什么会这样.请注意,我使用的是Windows 7.
使用“cmd /c start [...]”运行批处理文件将创建一个子进程,而不是直接运行批处理文件。
因此,您将无法访问其输出。为了使其工作,您应该使用:
String cmd = "C:\\workspace\\temp.bat";
Run Code Online (Sandbox Code Playgroud)
它可以在 Windows XP 下运行。