Catch when the Java process was completed

How can I catch when someone kills my application (java, but that doesn't matter) using taskmanager or taskkill console command?

I understand that I cannot catch this in my application, but maybe I can do this with some kind of hook from the OS (of course, windows). The easyhook library ( http://www.codeplex.com/easyhook ) may help me, but I can't find examples there. Our application often died on client servers, and I just want to find out who (or what) is killing it. We are sure that this is not an application problem, it seems that the java.exe process was killed from taskmanager

+4
source share
4 answers

You can do it:

Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { // run some code } }); 

If the VM crashes in native code, it will not be called. It will also not be called if the VM is stopped. This does not tell you why it closes. See Shutdown Hooks API Design . I do not think you can get more information than this.

Often in the past I used Java Wrapper . This is a separate process that starts and restarts Java processes, and it outputs the output from them, which can be useful if exceptions unexpectedly kill the process.

+2
source

You can also use java application using two applications. This is how it is done on many clean java produciton systems.

  • Your main application will make the .lock file in the standard lock and lock it exclusively.
  • Your other program (maybe call it janitor app) will lock () call it and keep waiting.

  • When the lock call returns, you can make sure your main application terminates. Now you can determine.

    Regarding the question β€œWhy Appliciton is killed”, technically you cannot find out (maybe 1 or 2 scenarios, but not all scenarios), AFAIK.

+1
source

you can use a second application that will run on the local computer and control your main application. when the main application goes down, it can do something (for example, restart it, register an event) c

0
source

There is no (standard) way to find out when your application will be killed by the task manager with Java.

The usual approach is to have a second application that runs the main application as a child process (use ProcessBuilder ). The wrapper will notice when the baby dies. For all common reasons for termination, set the exit code through System.exit() in the main application. Check the exit code in your wrapper. If this is not the one you explicitly asked, then someone killed the application or it crashed due to a VM error.

To distinguish between the two, check the output of the child application and find the VM crash dumps in the current directory (no matter what your application might be, it is usually the directory in which your application was installed).

0
source

All Articles