JDK-4302814 : no shutdown event handling in Windows with javaw.exe
  • Type: Bug
  • Component: hotspot
  • Sub-Component: runtime
  • Affected Version: 1.3.0
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_98,windows_nt
  • CPU: x86
  • Submitted: 2000-01-05
  • Updated: 2001-06-13
  • Resolved: 2001-06-13
Related Reports
Relates :  
Description
Name: skT88420			Date: 01/05/2000


java version "1.3beta"
 Java(TM) 2 Runtime Environment, Standard Edition (build 1.3beta-O)
 Java(TM) HotSpot Client VM (build 1.3beta-O, mixed mode)


/*
<pre>
 Bug Report
  no shutdown event handling in Windows with javaw.exe

[symptom]
 On Windows, with javaw.exe, the shutdown handler which has been registered
 by java.lang.Runtime#addShutdownHook() is not called in OS shutdown phase.

[Environment]
 OS: Microsoft Windows NT 4.0 SP4 Japanese version
 JVM:
 # java.exe -version
 java version "1.3beta"
 Java(TM) 2 Runtime Environment, Standard Edition (build 1.3beta-O)
 Java(TM) HotSpot Client VM (build 1.3beta-O, mixed mode)

[to reproduce]
 - compile attached class
 # javac ShutdownHandlerThread.java
 - make shortcut in the Windows' startup folder
    $(windir)\Profiles\$(login_user)\startmenu\program\startup\kick.lnk
   with link-point
     "C:\Program Files\JavaSoft\Jre\1.3\bin\javaw.exe" ShutdownHandlerThread
   and working folder
     "C:\$(directory where ShutdownHandlerThread.class is. Say, c:\Temp )"
 - log in Windows and shutdown
 - re-login and see the working folder

  The file "log.txt" should be created in the working folder but not.
  With Window shown version VM "java.exe", it works expectedly, "log.txt"
  is left after shutdown.

  I do not want to show this DOS prompt because it doesn't add anything
  useful to make my Java application which only does shutdown cleaning
  tasks.
</pre>
*/

// ShutdownHandlerThread.java
import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.IOException;

/**
<pre>
 * test program for new shutdown handling function in kestrel
 *
 * @author AKIMOTO, Hiroki, PFU Ltd.
 * @since 1.3Beta
 */
public class ShutdownHandlerThread extends Thread {
  public static void main(String[] args) {
    // create and register shutdown handler thread
    Runtime runtime = Runtime.getRuntime();
    ShutdownHandlerThread shutdownHandlerThread = new ShutdownHandlerThread();
    runtime.addShutdownHook(shutdownHandlerThread);

    try {
      // pause main thread to wait shutdown signal
      System.out.println("joined");
      Thread.currentThread().join();
    } catch (InterruptedException ex) {
      System.out.println("interrupted");
    }
    System.out.println("end of main thread (not reached)");
  }

  /**
   * shutdown handler body
   *   write a file when shutdown
   */
  public void run() {
    System.out.println("ShutdownHandlerThread is invoked.");
    try {
      // make footprint
      PrintWriter printWriter = new PrintWriter(new FileWriter("log.txt"));
      printWriter.println("ShutdownHandlerThread::run() is invoked.");
      printWriter.close();
    } catch (IOException ex) {
      ex.printStackTrace();
    }
  }
}
/*
</pre>
*/
(Review ID: 99595) 
======================================================================

Comments
WORK AROUND Name: skT88420 Date: 01/05/2000 use "java.exe" instead of "javaw.exe", though it has a problem on a look. ======================================================================
11-06-2004

EVALUATION I ran the program from java and I have to Ctrl+C to exit which invoked said run command. When run with javaw it never exits because program does not have any kind of a mechanism to exit. Add a simple timer based call & exit inside of prgram and the program will terminate with your run handler.. and write said file.. Put code inside of main loop: try { Thread.sleep(5); } System.exit(0); Closing as not a bug. gary.collins@East 2001-06-13
13-06-2001