Java - 捕获 PC 正在关闭事件,或同等事件
我创建了一个编辑一些 XML 文件的软件。打开文件时会生成一个锁定文件,以避免多个用户同时编辑该文件。
我现在希望能够在有人关闭电脑时删除这样的锁。 (该应用程序在 Linux 和 Windows 上运行。)
当尝试关闭当前部分时,是否有任何公共信号传递到虚拟机?
干杯,
Ste
I created a software which edits some XML files. When a file is opened a lock file is generated to avoid having multiple users editing the file at the same moment.
I would like now to be able to delete such a lock if someone turns the PC off. (The application runs on both Linux and Windows.)
Is there any common signal which is passed to the Virtual machine when attempting to close the current section?
Cheers,
Ste
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试添加 shutdown hook:
在这种情况下,
run
方法中的代码将在 JVM 终止之前执行。 (除非 JVM 被诸如kill -9 31337
之类的东西杀死。)相关问题:
You could try to add a shutdown hook:
In this case the code in the
run
-method will be executed before termination of the JVM. (Unless the JVM is killed with something likekill -9 31337
.)Related question:
标准 Runtime.addShutdownHook(...)
使用 JNI 的更高级技术,
如何让我的 Java 应用程序在 Windows 中可以很好地关闭吗?
Standard Runtime.addShutdownHook(...)
More advanced technique using JNI,
How do I get my Java application to shutdown nicely in windows?