Staff completion of the program - catches Listener Emergency - Runtime.getRuntime (). AddShutdownHook (shutdownHook)

And how to catch the situation when the program was stopped from the development environment, for example IntelijIdea?

  • Tried debazhit ?? - E1mir
  • Yes, I tried debugging, but I don’t get into these blocks. As a test, I use a program that creates a temporary file when the program is started, and when it completes it destroys it. - Roberto

1 answer 1

If you run the application in Intellij Idea and try to leave it by clicking on the red box.

enter image description here

Idea generates an interrupt signal from the operating system and sends it to the java process. This signal can be processed within the program. This is done as follows (valid for linux, for other OS the name of the signal may differ):

  Signal.handle(new Signal("INT"), new SignalHandler() { public void handle(Signal sig) { System.out.println("ой, меня кто то хочет остановить"); System.exit(0); } }); 

This code adds a handler for the Interrupt from keyboard system event, displays a message in the console, and stops the application.

  • Tell me, for Eclipse, the code will be the same or need add-ons? - Roberto
  • @VladislavOsipenkov I don’t know what kind of event it generates, but I think the principle is the same, you can experiment with the names of the signals. - Artem Konovalov
  • Thanks for the help, but I can’t find the appropriate signal from Idea in Windows. - Roberto