我遇到以下问题...我以这种方式通过Process
创建了一个ProcessBuilder
:
private ProcessBuilder processBuilder;
private Process process;
public void init() {
processBuilder = new ProcessBuilder(
"java", "-jar",
"bam.jar",
host,
);
processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
try {
process = processBuilder.start();
} catch (IOException e) {
e.printStackTrace();
}
}
并且我有谁应该杀死进程的功能:
public void stop() {
process.destroy();
try {
process.waitFor(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (process.isAlive()) {
try {
Thread.sleep(5000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
process.destroyForcibly();
}
}
但是杀死process
有时可以,但是有时不起作用。有想法吗?