'How to tell Java to not exit when interrupt signal (Ctrl+C) is received

I know how to shutdown Java application gracefully by adding shutdown hook, or how to handle signals like interrupt or TERM.

The problem here is, in C, when I handle the interrupt I can force the app to exit or to just print a message and continue working. I want to do something like this. When you press Ctrl+C, I ask you do you really want to exit (y,n).

However, in Java I see that all I can do is write something before exiting, so I will always exit no matter what. What is the solution to this problem?



Solution 1:[1]

You can run the java application as a background process by using the & in the end of the command.

This way the CTRL + C in your command line will not force the application to exit and you can still execute other commands in your command line tool.

#!/bin/sh
java -jar myApp.jar & 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Panagiotis Bougioukos