Java进程终止时捕获 [英] Catch when java process has been terminated

查看:118
本文介绍了Java进程终止时捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有人通过taskmanager或taskkill控制台命令杀死我的应用程序(java,但这并不重要),我怎么能抓住?

How can I catch when somebody kills my application (java, but it is not important) by taskmanager or by taskkill console command?

我知道我无法在我的应用程序中捕获这个但是我可以通过操作系统(当然是Windows)来实现这一点。也许easyhook库( http://www.codeplex.com/easyhook )可以帮助我,但我在那里找不到例子。
我们的应用程序经常死在客户服务器上,我只想知道谁(或什么)杀了它。我们确定它不是应用程序问题,似乎java.exe进程被taskmanager杀死了

I understand that I cannot catch this IN my application but maybe I can do this by some hook with OS (windows of course). Maybe easyhook library (http://www.codeplex.com/easyhook) can help me, but I can not find examples there. Our application often died on customers servers and I just want to know who (or what) kills it. We are sure that it is not application issue, it seems that java.exe process was killed from taskmanager

推荐答案

你可以这样做:

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

如果VM在本机代码中崩溃,则不会调用此方法。如果VM停止,它也不会被调用。它没有告诉你为什么它也会关闭。请参阅关闭挂钩API的设计。我不认为你可以得到更多的信息。

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.

过去我经常使用 Java Service Wrapper 。它是一个单独的进程,它启动并重新启动Java进程,并记录它们的输出,如果异常意外地终止进程,这可能很有用。

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.

这篇关于Java进程终止时捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆