这更像是一个评论,但一个流行的答案,但我的坏名声不允许我发表评论。
如果 stdout 或 stderr 溢出各自的缓冲区大小,您的进程将阻塞。
在 shell 中尝试你的命令,看看会发生什么,然后启动线程来读取 stdterr 和 stdout。
编辑:这是我在网上找到的,当我不需要 stdout 和 stderr 的内容,而只需要运行命令时,它对我有用。我在 processCC.waitFor() 之前调用 dropOutput。
private int dropOutput(final Process p) {
final Thread t = new Thread("drop") {
@Override
public void run() {
_dropOutput(p);
};
};
t.start();
int ev = 0;
Timer timer = null;
try {
timer = new Timer(true);
final InterruptTimerTask interrupter = new InterruptTimerTask(Thread.currentThread());
timer.schedule(interrupter, 2 /*seconds*/* 1000 /*milliseconds per second*/);
if (p.waitFor() != 0) {
ev = p.exitValue();
}
} catch (final InterruptedException e) {
// e.printStackTrace();
} finally {
timer.cancel(); // If the process returns within the timeout period, we have to stop the interrupter
// so that it does not unexpectedly interrupt some other code later.
Thread.interrupted(); // We need to clear the interrupt flag on the current thread just in case
// interrupter executed after waitFor had already returned but before timer.cancel
// took effect.
//
// Oh, and there's also Sun bug 6420270 to worry about here.
}
return ev;
}
/**
* Just a simple TimerTask that interrupts the specified thread when run.
*/
class InterruptTimerTask extends TimerTask {
private final Thread thread;
public InterruptTimerTask(final Thread t) {
this.thread = t;
}
@Override
public void run() {
thread.interrupt();
}
}
private void _dropOutput(final Process p) {
final InputStream istrm = p.getInputStream();
// final InputStream istrm = p.getErrorStream();
final InputStreamReader istrmrdr = new InputStreamReader(istrm);
final BufferedReader buffrdr = new BufferedReader(istrmrdr);
@SuppressWarnings("unused")
String data;
try {
while ((data = buffrdr.readLine()) != null) {
// System.out.println(data);
}
} catch (final IOException e) {
// do nothing
}
}