title: Detect when a Java program gets killed
body: <p>So I have a java program and sometimes it can hang (stop responding). This is due to an inner loop that I know is undetectable. But whenever that happens, I press X button on the window and it takes a little while until my OS kills the program. My IDE informs me that the exit code was -805306369 (0xCFFFFFFF) even though I never set it in the application.</p>
  
  <p>This can be really hard to debug if I haven't start the app in debugging mode in the first time. What I would like to do is to catch OS kill signal and print a crash report into a file that would show the stack trace of all the running thread. I can already do that manually if I launched my app with debug enabled and if I press pause in IDE when it hangs. I don't always launch my app with debug enabled so I'd like to have an automated solution.</p>
  
  <p>Is there any way I can do that? The only way I can think of right now is to have an extra thread detecting some kind of key shortcut or command and would print the current stack of all thread before killing the application for me. Of course doing it automatically when the app is killed would be better.</p>