如何让我的Java应用程序在Windows中很好地关闭? [英] How do I get my Java application to shutdown nicely in windows?

查看:160
本文介绍了如何让我的Java应用程序在Windows中很好地关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java应用程序,当用户选择Start-> Shutdown时,我想要很好地关闭它。我已经尝试通过Runtime.addShutdownHook(...)使用JVM关闭监听器,但这不起作用,因为我不能使用它的任何UI元素。

I have a Java application which I want to shutdown 'nicely' when the user selects Start->Shutdown. I've tried using JVM shutdown listeners via Runtime.addShutdownHook(...) but this doesn't work as I can't use any UI elements from it.

我也尝试在我的主应用程序UI窗口上使用退出处理程序,但据我所知,它无法暂停或停止关机。我怎样才能很好地处理关闭?

I've also tried using the exit handler on my main application UI window but it has no way to pause or halt shutdown as far as I can tell. How can I handle shutdown nicely?

推荐答案

前面提到的JNI方法可能会有效。

The previously mentioned JNI approach will likely work.

您可以使用JNA,它基本上是JNI的包装器,使其更易于使用。另一个好处是,它(至少在我看来)通常比原始JNI更快,更易于维护。您可以在 https://jna.dev.java.net/ 找到JNA

You can use JNA which is basically a wrapper around JNI to make it easier to use. An added bonus is that it (in my opinion at least) generally is faster and more maintainable than raw JNI. You can find JNA at https://jna.dev.java.net/

如果您只是在开始菜单中启动应用程序,因为您试图让它的行为类似于Windows中的服务,您可以使用此处的java服务包装器:
http://wrapper.tanukisoftware.org/doc/english/download。 jsp

If you're just starting the application in the start menu because you're trying to make it behave like a service in windows, you can use the java service wrapper which is found here: http://wrapper.tanukisoftware.org/doc/english/download.jsp

这篇关于如何让我的Java应用程序在Windows中很好地关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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