简体   繁体   English

Java程序停止时停止外部程序

[英]Stop external program when Java program is stopped

I'm executing an external program which runs alongside Java using this:我正在执行一个与 Java 一起使用的外部程序:

Process p = Runtime.getRuntime().exec("/path/to/binary");

When I stop the Java application, the external program continues to run but I want this to stop too.当我停止 Java 应用程序时,外部程序继续运行,但我也希望它停止。

To trigger the shutdown, you could add a shutdown hook to your Java application.要触发关闭,您可以向 Java 应用程序添加关闭挂钩。 See Runtime.addShutdownHook() .请参阅Runtime.addShutdownHook()

To effect the shutdown, you could either communicate to the external process a request to stop gracefully, or call Process.destroy()要实现关闭,您可以向外部进程传达正常停止的请求,或者调用 Process.destroy()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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