如何让 shutdown hook 在从 Eclipse 启动的进程上执行
我的应用程序中有一个关闭挂钩(使用 Runtime.getRuntime().addShutdownHook 创建)。 但是,如果我从 Eclipse 中启动该应用程序,当它关闭时,关闭挂钩不会执行。
我认为这是因为 Eclipse 向进程发送了相当于强制终止信号的信号,这不会导致关闭挂钩执行(相当于 Windows 上的 taskkill /F 或 Linux 上的kill -p),尽管我'我不太确定。
有谁知道如何解决这个问题? 我运行的是 Windows (Vista),我感觉这可能是 Windows 特定的问题,但我不确定。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我在主要方法的末尾使用了以下 hack 来解决这个问题:
I used the following hack at the end of my main method to get around the problem:
首先,您的申请是结束还是强制结束? 如果您强行结束它(通过停止按钮),此 Eclipse bug 报告 详细说明了为什么这可能不起作用。
如果做不到这一点,您关于这是 Windows 特定行为的看法可能是正确的。 我使用的是 Mac,所以无法确认,抱歉。 不过,我可以告诉您,以下测试程序确实按预期执行关闭挂钩。
运行时的 Javadocs #addShutdownHook 确实提到,当 JVM 中止而不是正常退出时,关闭挂钩不会运行,因此,您的假设可能是正确的。 话虽如此,这里有一些可以尝试的事情。 再次抱歉,我无法提前确认这些 - 这里没有 Windows。 (幸运的是!)
First off, is your application ending or are you forcibly ending it? If you are forcibly ending it (via the stop button), this Eclipse bug report has details about why this might not be working.
Failing that, you may be correct about this being Windows-specific behavior. I'm on a Mac so I can't confirm, sorry. However, I can tell you that the following test program does execute the shutdown hooks as expected.
The Javadocs for Runtime#addShutdownHook do mention that shutdown hooks do not run when the JVM is aborted, not exited normally, so again, you are probably correct in your assumptions. That being said, here are some things to try. Again, sorry I can't confirm these ahead of time -- no Windows here. (Blessedly!)
下面是一个可以在 Eclipse 外部运行的脚本,用于列出 Eclipse 下运行且可以终止的可用进程。
Here's a script that you can run outside of eclipse to list the available processes running under Eclipse that you could kill.
在 Windows 上,要以标准方式正常停止 Java 应用程序,您需要向其发送 Ctrl + C 。 这仅适用于控制台应用程序,但 Eclipse 使用
javaw.exe
而不是java.exe
。 要解决此问题,请打开启动配置、JRE 选项卡并选择“替代 JRE:”。 将出现“Java 可执行文件”组框,并允许输入备用可执行文件“java”。现在我们需要一个外部程序将 Ctrl-C 发送到具有隐藏控制台的进程。 我在此处找到了提示 和 在这里。 我们的程序附加到所需进程的控制台并发送控制台事件。
测试java程序:
终止它:
在Eclipse中测试程序输出:
On Windows to gracefully stop a java application in a standard way you need to send Ctrl + C to it. This only works with console apps, but Eclipse uses
javaw.exe
instead ofjava.exe
. To solve this open the launch configuration, JRE tab and select "Alternative JRE:". The "Java executable" group box appears and allows to enter the alternate executable "java".Now we need an external program to send Ctrl-C to a process with a hidden console. I found hints here and here. Our program attaches to the console of the desired process and sends the console event.
Test java program:
Terminating it:
Test program output in Eclipse:
我不知道如何解决这个问题,但 IntelliJ 在其“运行”对话框中添加了一个单独的按钮,它将以调用关闭挂钩的方式关闭虚拟机。 他们的调试器没有这个功能。
I'm not sure how to fix this but IntelliJ added a separate button to their 'Run' dialog which will shutdown the VM in a way that calls the Shutdown Hooks. Their debugger does not have this feature.
我现在被困在 websphere 中,看不到我在找什么。
但我确实记得 eclipse 有一个与在同一虚拟机中启动应用程序相关的运行配置选项。
您是否可以在与 eclipse VM 相同的 VM 中启动 java 应用程序?
但这个选择却让我忘记了。
I'm stuck in websphere at the moment, and don't see what I'm looking for.
But I do remember eclipse having a run configuration option related to launching the application in the same VM.
Is it possible your launching your java application in the same VM as the eclipse VM?
But the option slips my mind.
Sairam就在这里,通过调用System.exit(0)我们可以终止eclipse JVM并可以看到Shutdown hook结果
Sairam is right here, By calling System.exit(0) we can terminate eclipse JVM and can see Shutdown hook results