Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running code on program exit in Java [duplicate]

Is it possible to write a method that System.exit will call when you terminate a program?

like image 864
Alex Avatar asked Apr 21 '11 17:04

Alex


2 Answers

Use Runtime.getRuntime().addShutdownHook(Thread).

like image 52
Petar Minchev Avatar answered Oct 05 '22 11:10

Petar Minchev


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).

like image 38
TofuBeer Avatar answered Oct 05 '22 11:10

TofuBeer