Java Exit on Windows Shutdown

廉价感情. 提交于 2019-12-02 03:08:47
Kierrow

Assuming that the question you meant to ask is "how to detect when Windows is being shut down", here is how you do it:

You need a so-called "System Shutdown Hook", which is essentially a Thread who's run() method is executed whenever the Java virtual machine shuts down. The happens either when your program terminates or because of a system-wide event, such as a user log-off or a shut-down.

All you need to do is put this piece of code somewhere in the startup process of your program:

Runtime.getRuntime().addShutdownHook(
    new Thread(new Runnable() {
        @Override
        public void run() {
            // this is executed on shut-down. put whatever.
        }
    }));

I hope this answers your question.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!