我使用Java中的ProcessBuilder启动流程。可能会启动多个过程。如何停止流程?
public void terminalExecute(String builder) throws InterruptedException
{
System.out.println(builder);
String[] splits = builder.split(" ");
System.out.println(splits.length);
ProcessBuilder pb = new ProcessBuilder(splits[0],splits[1],splits[2],splits[3],splits[4],splits[5],splits[6],splits[7]);
Process p = null ;
pb.redirectErrorStream(true);
try {
p = pb.start();
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String s = "";
while((s = in.readLine()) != null){
System.out.println(s);
}
int status = p.waitFor();
System.out.println("Exited with status: " + status);
} catch (IOException e) {
e.printStackTrace();
}
}
在另一个类中,我将此函数称为: String cmd ="java -Xmx3024m -XX:MaxPermSize=512m -Xms1024m -cp "+ ConfReader.getAFFJarPath()+" core.StrWorkflow "+new BigInteger(serviceRequest.getBytes());
ProcessExecutor procExec = new ProcessExecutor();
procExec.terminalExecute(cmd);
我如何在此类中获取进程ID? 最佳答案
start()
的ProcessBuilder
方法返回Process
实例。您可以在其上调用destroy()
方法。
另请:https://docs.oracle.com/javase/7/docs/api/java/lang/Process.html