5

I'm executing an external program which runs alongside Java using this:

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.

Matt
  • 11,157
  • 26
  • 81
  • 110
  • 2
    possible duplicate of [How do I get rid of Java child processes when my Java app exits/crashes?](http://stackoverflow.com/questions/261125/how-do-i-get-rid-of-java-child-processes-when-my-java-app-exits-crashes) – Merlyn Morgan-Graham Jun 03 '11 at 20:48

2 Answers2

4

To trigger the shutdown, you could add a shutdown hook to your Java application. See Runtime.addShutdownHook().

To effect the shutdown, you could either communicate to the external process a request to stop gracefully, or call Process.destroy()

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
1

use shutdown hook : http://download.oracle.com/javase/1.4.2/docs/guide/lang/hook-design.html

Op De Cirkel
  • 28,647
  • 6
  • 40
  • 53