使用Java从BAT文件获取输出 [英] Get output from BAT file using Java
本文介绍了使用Java从BAT文件获取输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试运行.bat文件并获取输出.我可以运行它,但无法在Java中获得结果:
I'm trying to run a .bat file and get the output. I can run it but I can't get the results in 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);
}
结果为 null
.不知道为什么我得到这个.请注意,我使用的是Windows 7.
The result is null
. No idea why I get this. Note that I'm using Windows 7.
推荐答案
使用"cmd/c start [...]"运行批处理文件将创建一个子进程,而不是直接运行您的批处理文件.
Using "cmd /c start [...]" to run a batch file will create a sub process instead of running your batch file directly.
因此,您将无法访问其输出.要使其正常工作,您应该使用:
Thus, you won't have access to its output. To make it work, you should use:
String cmd = "C:\\workspace\\temp.bat";
它可以在Windows XP下运行.
It works under Windows XP.
这篇关于使用Java从BAT文件获取输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文