Вопрос

If I've got a Java program that can exit for various reasons, like:

  • because the main window, which is set to "exit on close", was closed

  • because there are some System.exit( 0 ) in the code

  • because there are no more window at all (and none was set to exit on close) but there's still several threads running then at one point there are only daemon threads running and hence the program exits.

And I've got a shutdown hook installed (which is running fine).

Is there any way to know, from my shutdown hook, what caused the Java program to exit?

(note that I'm not asking if it's a good idea or not to have System.exit(...) spread over the codebase: this is not what this question is about)

Basically I'd like to know if I'm forced myself to intercept every single possible JVM exit point and add infos there or if there's already a method allowing to do that.

Это было полезно?

Решение

You can add a SecurityManager which will be called on System exit (to determine if its allowed). You can save where this was called for later or deal with it in the SecurityManager.

Другие советы

Your shutdown hook will just run your runnable logic in a separate thread when the JVM is shutting down. You cant do anything more with this.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top