ShutdownHook
If you want to carry out Code while VM is quitted, you have the possibility to define and to announce one or more thread(s). This is then carried out while quitting.
Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { System.out.println("Die VM wird gerade beendet!"); } });