首页 \ 问答 \ 从ShutdownHook中查找程序退出的原因(Find, from a ShutdownHook, why a program exits)

从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.

更新时间:2023-02-03 22:02

最满意答案

您可以添加将在系统出口处调用的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.

相关问答

更多

相关文章

更多

最新问答

更多
  • 在csproj中使用appdata环境变量(Use appdata environment variable in csproj)
  • 从背景返回后,Skobbler Map崩溃(Skobbler Map crashes after returning from background)
  • 如何保持对绑定服务的轮询?(How to keep polling a bound service?)
  • ASP.NET单选按钮jQuery处理(ASP.NET radio button jQuery handling)
  • Linux上的FORTRAN图形库(FORTRAN graphic library on Linux)
  • 我们如何根据索引更新dynamodb表(不基于primary has和range key)(how can we update dynamodb table based on index(not based on primary has and range key))
  • 功能包装避免重复(wrap of functions avoid duplicating)
  • Android BroadcastReceiver和Activity.onPause()(Android BroadcastReceiver and Activity.onPause())
  • 无法使用phonegap 2.4在Android上播放录音(unable to play audio recordings on android using phonegap 2.4)
  • VS2015 + Resharper:不要使用C#6(VS2015 + Resharper: Don't use C#6)
  • 大学电脑四级对初学者来说要多久能过
  • 特殊字符删除?(Special characters remove?)
  • Android视频教程现在网上的都比较零散呢?有些太坑爹了,感觉老师就是在想当然的讲
  • 计算同一个表中不同行之间的差异[重复](Calculate delta's between different rows in same table [duplicate])
  • Javaweb开发,技术路线是什么?该怎么写?
  • JavaScript只在php代码中执行一次(JavaScript only executes once inside php code)
  • 不兼容的字符编码:ASCII-8BIT和UTF-8(incompatible character encodings: ASCII-8BIT and UTF-8)
  • Clojure(加载文件)给出错误(Clojure (load-file) gives an error)
  • 为具有瞬态scala依赖性的spring-xd项目优化gradle(Optimize gradle for spring-xd project with transient scala dependency)
  • 如何才能在Alpha测试模式下发布我的应用程序?(How can I publish my app in Alpha test mode only?)
  • “没有为此目标安装系统映像”Xamarin AVD Manager(“No system images installed for this target” Xamarin AVD Manager)
  • maven中的Scalatest:JUnit结果(Scalatest in maven: JUnit results)
  • 使用android SDK将文件直接上传到存储桶中的文件夹(Upload a file directly to a folder in bucket using android SDK)
  • 是否应将plists导入CoreData?(Should plists be imported to CoreData?)
  • java.lang.reflect.InvocationTargetException JavaFX TableView(java.lang.reflect.InvocationTargetException JavaFX TableView)
  • 根据唯一列值动态创建多个子集(Dynamically create multiple subsets based on unique column values)
  • 使用CSS可以使HTML锚标签不可点击/可链接吗?(Is it possible to make an HTML anchor tag not clickable/linkable using CSS?)
  • 嵌套的模板可能性(Nested template possibilities)
  • 任何方式在iOS7 +上以编程方式打开蓝牙(Any way to turn on bluetooth programmatically on iOS7+)
  • 如何为给定的SQL查询编写JPA查询(How I can write JPA query for given SQL query)