我已经尝试用谷歌搜索这个问题有一段时间了,但没有成功。我希望我的问题能在这里得到解决。
第一个函数:
public void startTFServer(Channel c) {
try {
ProcessBuilder procBuilder = new ProcessBuilder("tfs.exe");
procBuilder.redirectErrorStream();
Process proc = null;
System.out.println(Runtime.getRuntime().freeMemory());
proc = procBuilder.start();
System.out.println(Runtime.getRuntime().freeMemory());
StreamGobbler gobbler = new StreamGobbler(proc.getInputStream(), "STD_OUT");
gobbler.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
捕获进程输出的线程:
private class StreamGobbler extends Thread {
InputStream is;
String type;
private StreamGobbler(InputStream is, String type) {
this.is = is;
this.type = type;
}
@Override
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(type + "> " + line);
}
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
问题: 运行应用程序时,输入每次都会在同一位置中断。我使用“进程类”运行的应用程序是一个需要相当多内存才能运行的服务器,这可能是该进程无法完成加载我的应用程序的原因之一吗?我以为内存会耗尽,但我无法真正诊断它。
任何帮助将不胜感激!
最佳答案
我注意到一个问题:
procBuilder.redirectErrorStream();
这不是你想要的。这是一个 getter 方法,它告诉您redirectErrorStream 属性的值是什么。可能是您有错误并且您被阻止,因为它们未被读取。因此,需要使用setter方法:参见API redirectErrorStream(boolean)
procBuilder.redirectErrorStream(true);
关于Java进程在进程中途停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16907898/