Catch when java process has been terminated

后端 未结 4 529
有刺的猬
有刺的猬 2021-01-13 20:50

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

4条回答
  •  春和景丽
    2021-01-13 21:54

    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).

提交回复
热议问题