简体   繁体   中英

Process doesn't stop in Java

I have following problem...I created a Process via ProcessBuilder in this way:

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();
        }
    }

and I have function who should a kill process:

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();
    }
}

But killing process sometimes work, but sometimes doesn't work. Any idea?

had similar problem, resolved it by replacing processBuilder.start() with java.lang.Process process = java.lang.Runtime.getRuntime().exec("command java -jar some.jar");proccess.destroy();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM