从ShutdownHook中查找程序退出的原因(Find, from a ShutdownHook, why a program exits)
如果我有一个可以出于各种原因退出的Java程序,例如:
因为设置为“关闭时退出”的主窗口已关闭
因为代码中有一些System.exit(0)
因为根本没有窗口(并且没有设置为关闭时退出)但是仍有几个线程正在运行,然后在一个点上只有守护程序线程在运行,因此程序退出。
我安装了一个关机钩(运行正常)。
有没有办法知道,从我的关闭钩子,什么导致Java程序退出?
(请注意,我不会问是否将
System.exit(...)
传播到代码库是否是个好主意:这不是这个问题的意思)基本上我想知道我是否强迫自己拦截每个可能的JVM出口点并在那里添加信息,或者是否已经有一种允许这样做的方法。
If I've got a Java program that can exit for various reasons, like:
because the main window, which is set to "exit on close", was closed
because there are some System.exit( 0 ) in the code
because there are no more window at all (and none was set to exit on close) but there's still several threads running then at one point there are only daemon threads running and hence the program exits.
And I've got a shutdown hook installed (which is running fine).
Is there any way to know, from my shutdown hook, what caused the Java program to exit?
(note that I'm not asking if it's a good idea or not to have
System.exit(...)
spread over the codebase: this is not what this question is about)Basically I'd like to know if I'm forced myself to intercept every single possible JVM exit point and add infos there or if there's already a method allowing to do that.
最满意答案
您可以添加将在系统出口处调用的SecurityManager(以确定是否允许)。 您可以保存以后调用它的位置或在SecurityManager中处理它。
You can add a SecurityManager which will be called on System exit (to determine if its allowed). You can save where this was called for later or deal with it in the SecurityManager.
相关问答
更多-
TCP/IP模型是一个________。[2022-12-11]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
在Shutdownhook上使用JavaFX Application.stop()方法(Using JavaFX Application.stop() method over Shutdownhook)[2023-02-13]
只有在通过Platform.exit()退出应用程序时(或者如果Platform.implicitExit为true,最后一个窗口关闭时stop()才会调用stop() )。 如果调用System.exit() ,或者运行JVM的本机进程被中断(例如类似于nix的操作系统上的ctrl-C System.exit()则关闭钩子将执行,除了退出JavaFX应用程序的常用方式外。 stop()在FX应用程序线程上执行,因此访问UI元素是安全的(例如显示“保存未保存的更改”对话框等)。 关闭钩子在后台线程中运行,因 ... -
为什么不能关机?(Why won't this shutdownhook work?)[2022-07-15]
调度线程已经关闭,或者在您执行队列之前可能会关闭。 你需要找到另一种陷阱退出事件的方式,shutdown hook在那里,这样你可以在虚拟机退出时收拾,所以你真的不想在被调用的时候在UI中绑定更多的资源。 The dispatch thread is already shut down, or is likely to be shut down before your submission to the queue is executed. You need to find another way of t ... -
关闭钩子机制没有任何方法只为特定的退出代码运行关闭钩子。 您可以注册一个关闭钩子来检查退出代码, 如此处所讨论的那样 ,并根据它决定要做什么。 The shutdown hook mechanism doesn't have any way to run shutdown hooks just for particular exit codes. You could register a shutdown hook which checks the exit code, as discussed here, ...
-
logback的shutdownHook是否取消注册jmxConfigurator(Does logback's shutdownHook unregister the jmxConfigurator)[2022-11-04]
是的,它确实。 以下是调试器的证明: 但是,有一些限制:仅在1.1.3版本中可用 虽然文档说明了 就足够了,你必须指定class属性: 否则logback抱怨: ERROR in ch.qos.logback.core.joran.action.ShutdownHookAction - Missing class ... -
您可以添加将在系统出口处调用的SecurityManager(以确定是否允许)。 您可以保存以后调用它的位置或在SecurityManager中处理它。 You can add a SecurityManager which will be called on System exit (to determine if its allowed). You can save where this was called for later or deal with it in the SecurityManage ...
-
Java中没有启动挂钩。 你的选择是(按我推荐的顺序)。 使用您正在运行的框架的启动回调功能(即Servlets,Spring等)。 这还包括简单地让编写main方法的人给你一个回调。 使用您自己的main(String[])方法包装main(String[])方法,然后在从命令行调用main后委派。 在jar清单中创建一个带有Premain-Class定义的java代理库,然后在命令行上将代理添加到JVM。 最后两个选项要求您在命令行上添加或更改内容。 There is no startup hook i ...
-
对不起,我无法重现这个(OpenSUSE 12.3 x64,PyQt 4.9.6)。 我拿了你的代码,并在main()添加了一行window.show() (尽管你的评论说你正在“做window.show() ”)。 我也换了线 userRecvdFrom = newUsrFile(".")[0] 同 userRecvdFrom = newUsrFile.split(".")[0] 前者给出了运行时错误,因为newUsrFile是一个字符串而你无法调用它。 我还更改了正在监视的目录,因为 ...
-
自Log4j关闭以来,记录器可能不可用,这显然发生在JVM关闭钩子运行之前。 如果你需要在关机时记录一些东西,我建议你加入JBoss机制,例如使用@PreDestroy 。 然后你的代码变成了 @Stateless class Timer { @Timeout public void doTimeOut(){ //some code } @PreDestroy public void onShutdown() { Logger.ge ...