Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { //do your stuff } });