My test code is like following :
public static void main(String[] args) throws IOException, InterruptedException {
// TODO Auto-generated method stub
while(true) {
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable()
{
public void run()
{
System.out.println("Shutdown Hook is running !");
System.out.println("Application Terminating ...");
}
}));
System.out.println("Running");
}
}
I run the code in eclipse, go to debug tab where I can see it in running state.I do right click -> terminate .And shutdown hook won't execute.
Output printed :
Running
Running
Running
.
.
.
.
.
.
You can't handle that case. It seems Eclipse is sending a SIGKILL which is not part of a graceful shutdown, and thereby cannot be caught. See https://stackoverflow.com/a/2541618/3560873