Determine when the application is about to exit
public class Main { public static void main(String[] argv) throws Exception { Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { System.out.println("Do shutdown work ..."); } }); } }