Search code examples
javahandlershutdown

Java: how can I react to computer shutdown in windows xp?


Is it possible make some handler that will do something when user shutdown computer with Java on Windows XP (optional, win7)? How?

Thanks.


Solution

  • It is possible to add a shutdown hook to your Java program which is invoked if the JVM is shutdown for any reason (other than System.exit()) including O/S shutdown. Is that what you want?

    Use: java.lang.Runtime.addShutdownHook(Thread):

    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            // shutdown code here
            }
        });