Real'sHowTo AddThis Feed Button
Custom Search

Trap JVM shutdownTag(s): Varia


The Java virtual machine shuts down in response to two kinds of events:

JDK1.3

public class TrapBreak {

public static void main(String args[]) throws Exception{
    new TrapBreak().doit();
    }

   public void doit() throws Exception{
       Runtime.getRuntime().addShutdownHook(new Thread() {
              public void run() {
                    System.out.println("*** END ***");
                  }
              });

       while(true) { Thread.sleep(100); System.out.print("."); }
   }
}

To test it out on Windows, in DOS window, start the class with

C:\temp> java TrapBreak >out.log
Now close the DOS window by clicking on the upper-right X.

Now check the out.log file, you will find "*** END ***" written by our ShutdownHook.

..................*** END ***

blog comments powered by Disqus


If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2013
[ home ]