Runtime.getRuntime().addShutdownHook( new Thread()
            {
            public void run()
                {
                // stuff to do on shutdown
                }
            });