如何从Eclipse启动的进程上执行shutdown hook [英] How to get shutdown hook to execute on a process launched from Eclipse

查看:640
本文介绍了如何从Eclipse启动的进程上执行shutdown hook的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个关机钩子(使用 Runtime.getRuntime()创建)addShutdownHook )。但是,如果我从Eclipse中启动应用程序,那么在关闭时不会执行shutdown hook。



我认为这是因为Eclipse向进程发送等效的强制kill信号,这不会导致关闭挂钩执行(相当于taskkill / F在Windows上,或者在Linux上kill -p),虽然我不是绝对肯定的。



有没有人知道如何解决这个问题?我正在运行Windows(Vista),我有一种感觉,这可能是Windows特定的问题,但我不确定。

解决方案

我在主要方法的最后使用了以下hack来解决问题:

  if( Boolean.parseBoolean(System.getenv(RUNNING_IN_ECLIPSE))){
System.out.println(您正在使用Eclipse;在此控制台中单击+
,按ENTER键调用System。 exit()并运行shutdown程序。);
try {
System.in.read();
} catch(IOException e){
e.printStackTrace();
}
System.exit(0);
}


I have a shutdown hook in my application (created using Runtime.getRuntime().addShutdownHook). However if I launch the application from within Eclipse, when it is shut-down the shutdown hook doesn't execute.

I think this is because Eclipse sends the equivalent of a force-kill signal to the process, which doesn't cause the shut-down hook to execute (equivalent of taskkill /F on Windows or kill -p on Linux), though I'm not absolutely sure.

Does anyone know how to get around this? I'm running Windows (Vista), and I've a feeling it may be a Windows-specific issue, but I'm not sure.

解决方案

I used the following hack at the end of my main method to get around the problem:

if (Boolean.parseBoolean(System.getenv("RUNNING_IN_ECLIPSE"))) {
    System.out.println("You're using Eclipse; click in this console and " +
            "press ENTER to call System.exit() and run the shutdown routine.");
    try {
        System.in.read();
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.exit(0);
}  

这篇关于如何从Eclipse启动的进程上执行shutdown hook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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