这里用到一个第三方的包,是github开源的
地址在这https://github.com/zeroturnaround/zt-exec
maven依赖
<dependency>
<groupId>org.zeroturnaround</groupId>
<artifactId>zt-exec</artifactId>
<version>1.11</version>
</dependency>
参考代码如下
public void newInvokeShell(String...paramters) throws InterruptedException, TimeoutException, IOException {
new ProcessExecutor().command(paramters)
.redirectOutput(new LogOutputStream() {
@Override
protected void processLine(String line) {
if(line.contains("Submitted application")) {
xxx=line.substring(line.indexOf("application_"));
}
}
})
.execute();
}
他有很多种方式可以选择,具体可以看github的文档