flakes flakes -4 years ago 104
Java Question

Sending a signal to a Process from Java 9

I hear in java 9 they did a lot of work updating the Process API. Is there now a cleaner way to send a signal to a

Process
spawned by Java? The signals are used as a simple way to trigger actions in the process.

Before I had to use reflection to get a
pid
and then use
Runtime#exec
to send the
kill
command. I assume you still need to use
Runtime#exec
because signal may be OS dependent, but I'm curious if anyone knows a better way than this!

Process p = Runtime.getRuntime().exec(...);
...
Runtime.getRuntime().exec("kill -SIGUSR1 " + p.getPid());

Answer Source

signal may be OS dependant

Yes, windows does not have signals in the same sense posix OSes do.

but I'm curious if anyone knows a better way than this!

jnr-posix provides thin wrappers around libc, including kill.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download