我正在按以下方式启动一个流程。
try {
final Process mvnProcess = new ProcessBuilder("cmd", "/c", "mvn", "--version")
.directory(new File(System.getProperty("user.dir")))
.inheritIO()
.start();
System.exit(mvnProcess.waitFor());
} catch (final IOException ex) {
System.err.format(IO_EXCEPTION);
System.exit(1);
} catch (final InterruptedException ex) {
System.err.format(INTERRUPTED_EXCEPTION);
System.exit(1);
}
自从我调用inheritIO()
我期待子进程在控制台上的输出,但什么也没有出现。我在这里缺少什么?
Edit:我知道我可以使用mvnProcess.getInputStream()
并显式读取进程的输出,将其循环写入控制台(或任何地方)。然而,我不喜欢这个解决方案,因为循环会阻塞我的线程。inheritIO()
看起来很有希望,但显然我不明白它是如何工作的。我希望这里有人能对此有所启发。
也许这是一个从子进程中读取它的选项:
添加此代码后start()
你将把它打印到标准输出:
InputStream is = mvnProcess.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null)
{
System.out.println(line);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)