Is it possible to write a method that System.exit will call when you terminate a program?
Asked
Active
Viewed 4.3k times
5 Answers
6
Shutdown hooks are the answer... here is an article on them. They do not come without issues (some of them are discussed in the article).

TofuBeer
- 60,850
- 18
- 118
- 163
3
You can use a shutdown hook.
http://download.oracle.com/javase/6/docs/api/java/lang/Runtime.html#addShutdownHook(java.lang.Thread)
Note that shutdown hooks will not run if the VM aborts abnormally or Runtime.halt(int) is called.

Dev
- 11,919
- 3
- 40
- 53
-
Then, What to do In the Case of call to Runtime.halt(int) or abnormally aborts – guru_001 Jul 25 '16 at 13:44
-
1@guru_001 Nothing can be done, the VM halts abruptly similar to if it had crashed. – Dev Jul 25 '16 at 16:19
2
Look into shutdown hooks, see http://docs.oracle.com/javase/8/docs/technotes/guides/lang/hook-design.html

Dilum Ranatunga
- 13,254
- 3
- 41
- 52