Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can implant shutdown hook in JVM - see this example: <a href="http://www.crazysquirrel.com/computing/java/basics/java-shutdown-hooks.jspx" rel="nofollow">http://www.crazysquirrel.com/computing/java/basics/java-shutdown-hooks.jspx</a>. Though it may not work in some cases like system crash, someone pulling the server plug etc. :-)</p> <p>========================</p> <p><strong>Update</strong></p> <p>Here is relevant extract from <a href="http://download.oracle.com/javase/6/docs/api/java/lang/Runtime.html#addShutdownHook%28java.lang.Thread%29" rel="nofollow">Runtime API</a> about your scenarios:</p> <p>=> Logoff and shutdown should trigger the hook properly</p> <blockquote> <p>The Java virtual machine shuts down in response to two kinds of events:</p> <pre><code>The program exits normally, when the last non-daemon thread exits or when the exit (equivalently, System.exit) method is invoked, or The virtual machine is terminated in response to a user interrupt, such as typing ^C, or a system-wide event, such as user logoff or system shutdown. </code></pre> </blockquote> <p>=> Task Manager may not - and that's why you are not seeing your print statement</p> <blockquote> <p>In rare circumstances the virtual machine may abort, that is, stop running without shutting down cleanly. This occurs when the virtual machine is terminated externally, for example with the SIGKILL signal on Unix or the TerminateProcess call on Microsoft Windows. The virtual machine may also abort if a native method goes awry by, for example, corrupting internal data structures or attempting to access nonexistent memory. If the virtual machine aborts then no guarantee can be made about whether or not any shutdown hooks will be run.</p> </blockquote> <p>===================================================================</p> <p>I made the following changes to that example code and it works:</p> <ol> <li>Placed a fake pause to keep JVM alive long enough for you to trigger Windows logoff</li> <li>Created a file on C drive (make change accordingly) so I can inspect the result when I log back in</li> </ol> <p>Try it out...</p> <pre><code>package org.helios.util; import java.io.BufferedWriter; import java.io.FileWriter; public class ShutdownHook { public static void main(String[] args) { Hook hook = new Hook(); System.out.println("Running Main Application..."); Runtime.getRuntime().addShutdownHook(hook); for (int i = 0; i &lt; 50; i++) { //Pause for 4 seconds try { Thread.sleep(4000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Going back to sleep"); } System.out.println("Normal Exit..."); } private static class Hook extends Thread { public void run() { try { FileWriter fstream = new FileWriter("c:\\out.txt"); BufferedWriter out = new BufferedWriter(fstream); out.write("JVM Shutting down"); out.close(); } catch (Exception e) { System.err.println("Error: " + e.getMessage()); } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload