我想从Java中启动一个进程,读取其输出并获取其返回代码。但在它执行时,我想能够取消它。我首先启动进程:
ProcessBuilder pb = new ProcessBuilder(args);
pb.redirectErrorStream(true);
Process proc = pb.start();
如果我调用proc.waitFor(),直到进程退出之前我就不能做任何事情。因此,我认为我需要像这样做:
while (true) {
see if process has exited
capture some output from the process
decide if I want to cancel it, and if so, cancel it
sleep for a while
}
这样做对吗?有人能给我一个在Java中实现这个的例子吗?