i wanna create a java application that must perform some essential action (closing of file objects successfully or any other task) before it is terminated by the user using task manager or before the system is logged off (or shut down) by the user.
Is it possible in java????
Thanks in advance….
You can implant shutdown hook in JVM – see this example: http://www.crazysquirrel.com/computing/java/basics/java-shutdown-hooks.jspx. Though it may not work in some cases like system crash, someone pulling the server plug etc. 🙂
========================
Update
Here is relevant extract from Runtime API about your scenarios:
=> Logoff and shutdown should trigger the hook properly
=> Task Manager may not – and that’s why you are not seeing your print statement
===================================================================
I made the following changes to that example code and it works:
Try it out…