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 simply by java application too, by using two apps. This is how it is done in many pure java produciton systems
Your other program(maybe call it janitor app) will keep do a lock() call on it and keep on waiting.
When the lock call returns you can be sure that your main app is terminated. Now you can determine.
As far as "Why applicaiton is killed " question, technically you cannot find out(maybe 1 or 2 scenarios , but not all scenarios), AFAIK.
you can use a second application that will run on the local machine and monitor your main application. when the main application goes down it can do something (e.g restart it, log the event) c
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.
There is no (standard) way to figure out when your application is killed by the task manager with Java.
The usual approach is to have a second application which starts the main application as a child process (use ProcessBuilder
). The wrapper will notice when the child dies. For all the usual termination reasons, set an exit code via System.exit()
in your main app. In your wrapper, check the exit code. If it isn't one you explicitly set, then someone killed the app or it crashed because of a VM bug.
To distinguish those two, check the output of the child app and look for VM crash dumps in the current directory (whatever that may be for your app; usually it's the directory in which your app was installed).