如何让我的Java应用程序在Windows中很好地关闭?

时间:2022-06-12 02:31:01

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.

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

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

2 个解决方案

#1


3  

The previously mentioned JNI approach will likely work.

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

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/

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

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

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

#2


3  

As far as I know you need to start using JNI to set up a message handler for the Windows WM_QUERYENDSESSION message.

据我所知,您需要开始使用JNI为Windows WM_QUERYENDSESSION消息设置消息处理程序。

To do this (if you're new to Windows programming like me) you'll need to create a new class of window with a new message handling function (as described here) and handle the WM_QUERYENDSESSION from the message handler.

要做到这一点(如果你是像我这样的Windows编程新手),你需要创建一个带有新消息处理函数的新窗口类(如此处所述),并从消息处理程序处理WM_QUERYENDSESSION。

NB: You'll need to use the JNIEnv::GetJavaVM(...) and then JavaVM::AttachCurrentThread(...) on the message handling thread before you can call any Java methods from your native message handling code.

注意:您需要在消息处理线程上使用JNIEnv :: GetJavaVM(...)然后使用JavaVM :: AttachCurrentThread(...),然后才能从本机消息处理代码调用任何Java方法。

#1


3  

The previously mentioned JNI approach will likely work.

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

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/

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

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

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

#2


3  

As far as I know you need to start using JNI to set up a message handler for the Windows WM_QUERYENDSESSION message.

据我所知,您需要开始使用JNI为Windows WM_QUERYENDSESSION消息设置消息处理程序。

To do this (if you're new to Windows programming like me) you'll need to create a new class of window with a new message handling function (as described here) and handle the WM_QUERYENDSESSION from the message handler.

要做到这一点(如果你是像我这样的Windows编程新手),你需要创建一个带有新消息处理函数的新窗口类(如此处所述),并从消息处理程序处理WM_QUERYENDSESSION。

NB: You'll need to use the JNIEnv::GetJavaVM(...) and then JavaVM::AttachCurrentThread(...) on the message handling thread before you can call any Java methods from your native message handling code.

注意:您需要在消息处理线程上使用JNIEnv :: GetJavaVM(...)然后使用JavaVM :: AttachCurrentThread(...),然后才能从本机消息处理代码调用任何Java方法。