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.

Tim Cooper
  • 151,519
  • 37
  • 317
  • 271
Matt
  • 10,767
  • 25
  • 78
  • 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
  • 82,182
  • 10
  • 99
  • 146
1

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

Op De Cirkel
  • 27,483
  • 6
  • 38
  • 53