我正在尝试远程执行.bat文件并实现以下代码行:
ProcessBuilder processBuilder = new ProcessBuilder(command);
final Process process = processBuilder.start();
InputStream stderr = process.getErrorStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
process.waitFor();
System.out.println("Waiting ...");
System.out.println("Returned Value :" + process.exitValue());
但我的程序在循环中陷入困境。 它显示的错误是:
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported. Defaulting to Windows directory.
它永远不会退出while循环。但是它成功执行了脚本。 任何形式的帮助表示赞赏。感谢
答案 0 :(得分:3)
你需要确保你也在处理stderr,你应该在不同的线程中处理这两个流。
阅读this并确保您遵循所有建议。
编辑:看看你编写的代码,你似乎已经从这篇精确的文章中复制了代码。实际上,它看起来像列出4.3(MediocreExecJavac.java)。
答案 1 :(得分:2)
在.start()之前添加redirectErrorStream(true)并读取getInputStream()。这将读取stderr并将其与stdout合并为一个可以使用getInputStream()读取的响应。
ProcessBuilder processBuilder = new ProcessBuilder(command);
processBuilder.redirectErrorStream(true);
final Process process = processBuilder.start();
InputStream stderr = process.getInputStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
process.waitFor();
System.out.println("Waiting ...");
System.out.println("Returned Value :" + process.exitValue());