I have a situation in which I want to perform some task when the user signals the OS(in my case only Windows) to shutdown.
I have tried using java shutdown hooks. The problem I face is that when I exit the program using System.exit(0);
, the shutdown hooks are called but when I directly shutdown the computer, they aren't.
This is the code I have used for the shutdown hooks-
Runtime.getRuntime().addShutdownHook(new JVMShutdownHook()); //in main method
//within the main java class
private static class JVMShutdownHook extends Thread {
@Override
public void run() {
//perform tasks
}
}
Is there any way to interact with the OS(I'm assuming some native code) so that it allows my program to exit gracefully?
Any help would be greatly appreciated.