Reputation: 2713
I have a Java app that creates an external process and reads the process' stdout through an InputStream. I need to be able to kill the process when I am done with it. Is there a way to send a SIGINT signal to this process? (as if I pressed Ctrl+C from the console).
The external process is not my code and I cannot modify it.
Upvotes: 24
Views: 34439
Reputation: 4665
For other people arriving here from Google if you want to send the signal to the current Process (aka JVM) you can use sun.misc.Signal.raise()
method. Personally I need it because of a JNI library that I am using.
Upvotes: 6
Reputation: 749
Are you running the external program as a java.lang.Process
? As the Process class has a destroy()
method.
Upvotes: 2
Reputation: 220902
Can you send kill -SIGINT <pid>
to the process (given that you know the process ID):
Runtime.getRuntime().exec("kill -SIGINT 12345");
Of course, that would make for a platform-dependent solution... Potentially, you'll be able to use this tool, although it is in "sandbox mode". But it might give you an idea:
http://commons.apache.org/sandbox/runtime/
See also this related question here:
how can I kill a Linux process in java with SIGKILL Process.destroy() does SIGTERM
Upvotes: 18
Reputation: 78
Unfortunately there isn't a native way to send an arbirtray signal in Java.
I agree with Lukas in using Runtime.exec() or you could use something like Posix for Java library.
Upvotes: 2