我试图在我的应用程序中从外部应用程序获取控制台输出。当我从我的代码中调用externall应用程序时,它会挂起消息:
配置记录...
从以下位置配置log4j:C:\ GPAT \ log4j.cfg
没有任何反应。我通过互联网搜索,似乎它可能是线程问题。但我无法修改此外部应用程序,我必须通过log4j。我读了这样的外部应用程序:
StringBuffer output = new StringBuffer();
try {
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(GSATCommand);
BufferedReader input = new BufferedReader(new InputStreamReader(proc.getInputStream()));
System.out.println("Test running...");
String line = null;
while ((line = input.readLine()) != null) {
System.out.println(line); // Writes the test output to console
output.append(line); output.append("\n");
}
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
System.out.println("Test successfully executed");
} catch (Throwable t) {
t.printStackTrace();
}
try {
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(GSATCommand);
BufferedReader input = new BufferedReader(new InputStreamReader(proc.getInputStream()));
System.out.println("Test running...");
String line = null;
while ((line = input.readLine()) != null) {
System.out.println(line); // Writes the test output to console
output.append(line); output.append("\n");
}
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
System.out.println("Test successfully executed");
} catch (Throwable t) {
t.printStackTrace();
}
感谢阅读。
答案 0 :(得分:2)
您需要在单独的线程中从生成的进程中使用stdout和stderr,以防止阻塞行为(您生成的进程将写入缓冲区,并阻止这些缓冲区未被清空你的消费过程)。
请参阅this answer的第2段以获取更多详细信息以及指向合适修订的链接。