Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 4486580
Votes 16
Synopsis javaw and shutdown hooks (referring to #4302814)
Category hotspot:runtime_system
Reported Against b05 , 1.3.1
Release Fixed
State 6-Fix Understood, request for enhancement
Priority: 4-Low
Related Bugs 4302814
Submit Date 31-JUL-2001
Description


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

Bug #4302814 describes the problem. It is closed as "not a bug".

I disagree and think it is a bug. I hope it can be re-opened.

Why should shutdown hooks be invoked for "java", but not for "javaw" processes?
How does the general concept of "javaw" differ from "java" in this respect to
justify a different behavior of java.lang.Runtime?

Think of GUI apps, which are preferably launched using "javaw". When the user
logs out or shuts down the system, such apps have no chance to flush some
files, store some settings, perform some cleanup etc. The only workaround is to
launch the app using "java", which is not a viable alternative because it opens
an ugly terminal window.
(Review ID: 126995) 
======================================================================
Posted Date : 2006-04-18 17:38:33.0
Work Around




Use "java", not "javaw", see above.
======================================================================
Evaluation
Will work with VM group to see what clarifications we can make in the 
documentation regarding this issue.
  xxxxx@xxxxx   2002-02-14

See the comments on BugParade:
http://developer.java.sun.com/developer/bugParade/bugs/4486580.html
  xxxxx@xxxxx   2003-10-07
Comments
  
  Include a link with my name & email   

Submitted On 20-FEB-2002
Alindstrom
Well, the workaround is not sufficient since the "nice" 
terminal window shows up again. It seems like the java app 
does not have a handle to the JVM, so when closed the JVM 
continues running using 10-11 MB memory in win2k.

I'll continue to look for a solution on this .. 


Submitted On 07-SEP-2002
javarules.com
For the sake of the individual who wrote this and others who 
may stumble upon it, this bug report should be closed. It is 
much ado about nothing (based on a faulty program). 
Shutdown hooks work fine when using javaw. For example, 
execute the following program using javaw Test and you will 
see that the file is written to the current working directory.

import java.io.*;          
public class Test {
   public static void main(String[] args) {
    Runtime.getRuntime().addShutdownHook(new Thread() {
         public void run() {
              try{ PrintWriter pw = new PrintWriter(
                                    new FileOutputStream("test.txt"));
                   pw.println("SHUTDOWN HOOKS RUNNING");
                   pw.close();
              } catch(Exception e) {}
             System.err.println("SHUTDOWN HOOKS RUNNING");
        }
     });
    }
}

Of course, the System.out.println message will not appear, 
but that is only because there is no console.



Submitted On 06-OCT-2003
bsampieri
While I agree that java.exe and javaw.exe should behave the
same in this regard, maybe the reason is that javaw.exe is
more suited for GUI apps, and those could therefore use a
window listener on the GUI frame/window to handle cleanup?


Submitted On 14-OCT-2003
mad_frog
To javarules.com
Modify your code this way:

import java.io.*;          
public class Test {
   public static void main(String[] args) {
    Runtime.getRuntime().addShutdownHook(new Thread() {
         public void run() {
              try{ PrintWriter pw = new PrintWriter(
                                    new FileOutputStream("test.txt"));
                   pw.println("SHUTDOWN HOOKS RUNNING");
                   pw.close();
              } catch(Exception e) {}
             System.err.println("SHUTDOWN HOOKS RUNNING");
        }
     });

    //!
    Thread.currentThread().sleep(600000);

    }
}

Run this code using javaw.exe in Windows 2000 and Log Off 
Windows while the thread is sleeping. The shutdown hook will 
not be executed. I had to write my own native launcher that 
uses java.exe and hides the console window using Windows 
technique because of this issue.


Submitted On 15-OCT-2003
gary.craig
Using java.exe instead of javaw.exe is not a reasonable
workaround.
Javaw should be fixed to behave as documented.
This issue exists on XP (at least) in addition to W2000.


Submitted On 03-FEB-2004
gary.craig
This also exists in 1.4.2_02, on XP


Submitted On 23-FEB-2004
mthornton
If the application concerned has windows then using 
java.exe doesn't work either.  The shutdown hook at 
logoff only works for applications that have no gui. Nor 
is it possible to listen for events on the frame/window --
- no close event is received at logoff.
For applications with a gui, Windows send a message 
to the applications message loop which java current 
ignores, but this is the ONLY way the JVM will be 
notified of impending process termination due to user 
logoff or operating system shutdown.
So javarules.com and bsampieri, neither of you have 
properly understood the problem described in this bug 
report. Yes, shutdown does work if you exit all (non 
daemon) threads or if you call System.exit. The 
problem is it does not run in response to external 
messages from the operating system.


Submitted On 23-FEB-2004
mthornton
To behave properly the JVM should process the 
WM_ENDSESSION message. It should not return from 
this message until all shutdown tasks have been 
completed. The WM_QUIT message should also be 
handled in this way.


Submitted On 24-FEB-2004
mthornton
On further investigation it seems that processes with 
message loops may receive both a signal and the 
WM_ENDSESSION message. However the process is 
liable to abrupt termination as soon as the message 
handler returns, and thus before the shutdown hooks 
have completed (or even started). The JVM should be 
changed so that both the signal and the 
WM_ENDSESSION message can initiate shutdown 
processing and that both block until that processing is 
complete. It is also likely that the timing of signals, 
WM_ENDSESSION messages and process 
termination varies between versions of Windows 
(Windows 9x is documented as having slightly different 
behaviour).
This problem should be reclassified as a bug not an 
RFE. An RFE would be the ability to choose the 
response to a WM_QUERYENDSESSION message 
(e.g. decline the request for logoff).


Submitted On 15-APR-2006
cowwoc
I tracked down the problem. Java's  WindowProc handles WM_ENDSESSION just fine, but it does not actually wait for the shutdown hooks to finish executing before returning.

The trick is this: if you have a JFrame visible, shutdown hooks will run to termination (Java is waiting on the JFrame to shut down) whereas if you have no visible windows, the application will get terminated immediately without running the shutdown hooks.


Submitted On 15-APR-2006
cowwoc
I finally found a workaround for this issue!

On initialization, run:

JWindow shutdownHookWorkaround = new JWindow();
shutdownHookWorkaround.setSize(0, 0);
shutdownHookWorkaround.setVisible(true);
Runtime.getRuntime().addShutdownHook(shutdownHook);

then inside your shutdown hook, run:

EventQueue.invokeLater(new Runnable()
{
  public void run()
  {
    shutdownHookWorkaround.setVisible(false);
    shutdownHookWorkaround.dispose();
  }
});


Submitted On 10-APR-2007
The workaround from cowwoc works for me in that way, that i receive the shutdown signal, but the jvm is still shut down immediately :-/ When using java (not javaw) the Windows message for terminating the application comes up and the jvm waits until the shutdown is comlete. So, i think the problem still exists...


Submitted On 03-JUL-2008
I am having the exact same issue here and it is really frustrating. My issue occurs because I have a SystemTray application using the JDIC module. When I run it with java.exe the shutdown hooks run to completion. When I run using Javaw.exe the shutdown hook isn't called at all. This is because there isn't a visible window in the system to catch the window shutdown message.

When I use the work around of creating the JWindow of size 1 the shutdown hook is invoked but rarely completes because the JVM shuts down too quickly. :(



PLEASE NOTE: JDK6 is formerly known as Project Mustang