How can I catch when somebody kills my application (java, but it is not important) by taskmanager or by taskkill console command?
I understand that I cannot catch t
You can do this:
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
// run some code
}
});
If the VM crashes in native code, this won't get called. Nor will it get called if the VM is halted. It doesn't tell you why it's shutting down either. See Design of the Shutdown Hooks API. I don't think you can get more information than that.
Often in the past I've used the Java Service Wrapper. It's a separate process that starts and restarts Java processes and it logs the output from them, which can be useful if exceptions unexpectedly kill the process.