流程不会在Java中停止

时间:2018-10-12 10:12:53

标签: java process runtime kill

我遇到以下问题...我以这种方式通过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有时可以,但是有时不起作用。有想法吗?

0 个答案:

没有答案