Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore SIGINT in Java

Tags:

java

posix

I'm using a Java wrapper for a native shared library on Unix (JRI). The native library (a C based REPL implementation for R) handles SIGINT internally. When using the Java wrapper, the Java application quits when I send a SIGINT to the process using:

kill -SIGINT pid

I'd prefer for the SIGINT to be entirely handled by the native library internally.

Is there an easy way to make Java completely ignore a SIGINT but still have the native library receive it?

Addition:

Preferably this would work on Unix and OSX.

like image 673
Tristan Avatar asked Nov 15 '25 02:11

Tristan


1 Answers

There is no standard API for it, but if you are using the Sun JDK, you can use sun.misc.Signal and sun.misc.SignalHandler. Example from the docs:

SignalHandler handler = new SignalHandler () {
    public void handle(Signal sig) {
        ... // handle SIGINT
    }
};
Signal.handle(new Signal("INT"), handler);
like image 133
Wouter Coekaerts Avatar answered Nov 17 '25 18:11

Wouter Coekaerts



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!