8

As the subject states, is there a way to disable CTRL-C (and other signals) so that they don't terminate the application?

Jin Kim
  • 16,562
  • 18
  • 60
  • 86
  • 2
    possible duplicate of [Handle signals in the Java Virtual Machine](http://stackoverflow.com/questions/40376/handle-signals-in-the-java-virtual-machine) – Johan Sjöberg Mar 14 '12 at 16:19
  • 2
    You can disable some signals, but not all e.g. `kill -9` ;) – Peter Lawrey Mar 14 '12 at 16:22
  • @Johan That article explains some of the difficulty I am having. More specifically, it appears that the JVM intercepts signals calls shutdown hooks (registered via Runtime.addShutdownHook) before terminating. However it does not seem to describe how/if it is possible to *disable* signals (or at least prevent them from terminating the application). – Jin Kim Mar 14 '12 at 17:10

3 Answers3

6

EDIT2: this may also help: Capture SIGINT in Java

This and this appear to be what you want.

Ctrl-C and other command send "signals" to your program, and if you write code to "catch" catch those signals, you can change how your program reacts. Ideally, you would want ctrl-c to terminate the program, as it's what the user expects, so keep in mind that you should tell the user why it's not terminating if you change the program's response.

Edit3: Cat's suggestion of a keylistener is your next best bet if you can't catch and dismiss the event yourself. Halway down the page is an example of how to do it for java in linux/Windows - it's not a portable thing. Essentially, your program will be handling keyboard events instead of the console sending the keybaord events to it. This means the command line is bypassed, and cannot read the ctrl-c call. I'd write code, but I'm at work now; Wednesdays are a long day for me. If you haven't gotten it by the time I get home, I'll take a look at writing some examples.

Community
  • 1
  • 1
Marshall Conover
  • 855
  • 6
  • 24
  • Thanks for your assistance, however those links refer to setting up shutdown hooks, which the JVM calls before terminating the application. The application is still terminated, which is what I'm trying to prevent. – Jin Kim Mar 14 '12 at 17:17
  • Try the IBM one I just added. Sorry, I'm in a class; I'll be able to take a closer look after I'm out, but that seemed to be the best article. – Marshall Conover Mar 14 '12 at 17:51
  • 1
    Referred IBM article page says that it cannot be displayed - 404 :( – Mike Jul 02 '15 at 09:53
  • @Mike - thanks for the heads-up! I've removed it, as I don't think it was essential, and I couldn't find the corresponding article on their site. – Marshall Conover Jul 02 '15 at 17:59
2

You need to use a SignalHandler to intercept the SIGINT signal triggered by a CTRL-C. The CTRL-C will be handled at the OS level assuming you don't want the VM to receive the signal.

IBM DeveloperWorks has an article on doing this - http://www.ibm.com/developerworks/ibm/library/i-signalhandling/?open&l=409,t=grj,p=jsh

Oracle link - http://www.oracle.com/technetwork/java/javase/signals-139944.html

wafflemkr
  • 49
  • 7
  • Neither of those links seem to work which is a pity--they sound really useful. I did just come across http://ringlord.com/dl/Signals-in-Java.pdf which may or may not be the same stuff you were referring to. Uses private Sun API calls. – Bill K Nov 20 '14 at 01:43
2

I would assume if you started a KeyListener to handle the event of ctrl+C being pressed it should either negate the 'natural' response or call a subroutine that you designed instead.

cat
  • 91
  • 10