程序是否识别出上次崩溃了?
让(Java)程序识别它上次运行时崩溃并显示一条消息“看起来这个程序上次崩溃了”的最佳方法是什么。在此处报告此问题:[email protected] ...."
有推荐的方法吗? (不好?)我的想法是:
- 让程序在启动时存储一个临时密钥文件,然后在定期关闭时删除它。如果该文件在启动时存在,则显示该消息。
- 在这种情况下,识别死锁并存储“错误文件”。如果启动时存在“错误文件”,请显示错误消息并将文件移至存档或类似文件中。
What is the best way to have a (Java) program recognize it crashed last time it ran and show a message along the lines of "it looks like this program crashed on you last time. Report this problem here: [email protected] ...."
Is there a recommended way of doing this? (Bad?) ideas I had would be:
- Have the program store a temporary key file at startup and then delete it when closing regularly. If the file exists at startup, show the message.
- Recognize deadlocks and store an "error file" in this case. If an "error file" exists at startup, show the error message and move the file into an archive or something similar.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Java 程序崩溃的原因有以下三个:
main
中的try-catch
可以轻松解决这个问题。main
中捕获。我通常在main
中捕获Throwable
。请参阅下面的模板。Thread.setDefaultUncaughtExceptionHandler()
。new File(...).deleteOnExit()
在某处创建一个标志文件。如果有机会,Java 会为你清理它。死锁的问题是如何检测是否存在死锁。我还没有找到一致的方法来做到这一点。
There are three reasons why a Java program can crash:
try-catch
inmain
.main
also. I usually catchThrowable
inmain
. See below for a template.Thread.setDefaultUncaughtExceptionHandler()
.new File(...).deleteOnExit()
. Java will clean it up for you if it gets a chance.The problem with deadlocks is how to detect that you have a deadlock. I haven't seen a consistent way to do that, yet.
因未捕获的异常而崩溃?使用 Thread.setDefaultUncaughtExceptionHandler,并将消息显示为崩溃的一部分。
关于第一个想法,如何处理同时运行的应用程序的多个实例? (还要考虑多用户环境)。
识别死锁 - 死锁问题出现的频率如何?我想你可以监视 线程状态 在所有“关键”线程上。
然后,您有外部力量杀死了应用程序,它们是否应该被视为您应该报告的问题?毕竟,在这种情况下,您的应用程序没有错误。
最后,始终以日志的形式存储“错误文件”。使用正确的日志记录框架(即 Java 日志记录或 Log4J)。您可以检查该文件的最后几行是否有应用程序正常退出的信号,但在多实例环境中您需要再次小心。
Crash as in an uncaught exception? The use a Thread.setDefaultUncaughtExceptionHandler, and display the message as part of the crash.
On the first idea, how do you handle multiply instances of the applications running at the same time? (Also think about multi-user environments).
Recognize deadlocks - How often are deadlocks the problem? I guess you could monitor the thread states on all the "key" threads.
You then have external forces killing the application, should they be considered a problem that you should report? After all your application was not at fault in this case.
Finally, always store an "error file" in the form of a log. Use a proper logging framework (i.e. Java Logging or Log4J). You could check the last lines of this for a signal that the application exited normally but again you will need to be careful in multi-instance environments.
您提出的第一个解决方案的一个变体在 Un*x 上对于进程来说很常见:在启动时将正在运行的进程的 pid 文件存储在文件中。当程序再次启动时,您可以检查该文件是否仍然存在(即使具有该 pid 的进程正在运行)。
对于 Java,您可能可以使用 ThreadMXBean 中定义的 Threadid 来适应这个想法。但任何文件都可以。包含您建议的密钥的文件似乎是一个足够好的方法。您还可以在其中添加一些有用的信息,例如上次执行时间。如果启动时它仍然存在,则程序没有完全停止。
它还可能成为类似于启动日志文件的东西,用于跟踪程序事件,包括启动和干净停止,也许还有锁定。
A variant of the first solution you propose is common enough on Un*x for processes: store the pid file of a running process in a file at startup. When the program is launched again you can check if this file still exists (and even if the process with this pid is running).
With Java you could probably adapt this idea using Threadid defined in ThreadMXBean. But any file would do. A file that contains a key as you propose seems a good enough way. You could also put some usefull information in it like last execution time. If it's still there at startup the program didn't stopped cleanly.
It could also become something like a launch log file that trace program events including startup and clean stops, and maybe locks.
我所做的是将 System.err 重定向到一个文件,以便任何错误消息(如崩溃)最终都会出现在我稍后可以处理的文件中...
执行此操作的代码非常简单...
What I do is redirect System.err to a file, so that any error message (like crashes) end up in a file I can later process...
The code to do this is quite simple...
我要在这里模仿马科斯。创建一个配置或日志文件,其中将包含程序的最后一条错误消息和上次运行日期。然后在程序加载期间读取该文件。
I'm going to mimic Marcos here. Create a configuration or log file that will host the last error message and last run date of the program. Then read that file during program load.
其中许多答案都是关于跟踪导致应用程序停止工作的异常。
另一种可能性是应用程序刚刚退出(即用户终止它、计算机关闭、断电等)。我认为你的临时关键想法会起作用。这类似于文本编辑程序(例如 vi 或 Word)如何自动创建正在编辑的文件的特殊副本。打开后,它会检查特殊副本是否存在,并询问您是否要恢复该文件。
Many of these answers are about tracking exceptions that caused your app to quit working.
Another possibility is that the application just quit (i.e. user killed it, computer shutdown, power outage, etc.). I think your temporary key idea will work for that. It's similar to how text editing programs, such as vi or Word, automatically create a special copy of the file being edited. Upon opening it checks to see if the special copy exists and asks if you want to recover the file.