7

Is there any possible way to add swing into a shutdown hook (that is, display a popup upon the VM shutting down)?

I realize that if I try to make a new JFrame, it will give me an error, as it tries to register a shutdown hook, which fails as the VM is already shutting down. I'm just wondering if there is in fact any way around this

Alex Coleman
  • 7,216
  • 1
  • 22
  • 31
  • 3
    I would say not, & even if you coud, I'd recommend that you dont – MadProgrammer Aug 25 '12 at 07:26
  • @MadProgrammer I have looked around, and couldn't find anything, but I was thinking it should be possible as many applications give you a popup window, not just upon closing, but like if you shutdown the computer (which in turn shuts down the Java VM) – Alex Coleman Aug 25 '12 at 07:28
  • I had a program recently throw an exception because a developer was accessing a Swing Component within a shut down hook (under Java 7 I think). I'd say that you might have an issue trying to follow this concept. Part of the problem is not knowing what state the JVM is actually in, or the order in which the shut down hooks are called – MadProgrammer Aug 25 '12 at 07:51
  • @AlexColeman It is certainly possible to show a popup window, but not via a shutdown hook as you seem to be assuming. Usually it is done in the window-closing event. – user207421 Aug 26 '12 at 07:04
  • 1
    On Macintosh, you can have an `ApplicationListener` which has an `public void handleQuit(ApplicationEvent)` method. If you want to support/have a Windows machine this won't work because it's in the `com.apple` package. – 11684 Aug 27 '12 at 15:17

5 Answers5

14

You really shouldn't be doing this. From the Runtime.addShutdownHook specification:

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

  • 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.

...

Shutdown hooks run at a delicate time in the life cycle of a virtual machine and should therefore be coded defensively. They should, in particular, be written to be thread-safe and to avoid deadlocks insofar as possible. They should also not rely blindly upon services that may have registered their own shutdown hooks and therefore may themselves in the process of shutting down. Attempts to use other thread-based services such as the AWT event-dispatch thread, for example, may lead to deadlocks.

Shutdown hooks should also finish their work quickly. When a program invokes exit the expectation is that the virtual machine will promptly shut down and exit. When the virtual machine is terminated due to user logoff or system shutdown the underlying operating system may only allow a fixed amount of time in which to shut down and exit. It is therefore inadvisable to attempt any user interaction or to perform a long-running computation in a shutdown hook.

...

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.

Specific warnings here that suggest you not do this:

  1. "Shutdown hooks should also finish their work quickly."

    Relying on anything that might take a while to do its work, or blocking indefinitely on user-input like JOptionPane dialogs, is not what you should be doing in your shutdown hook.

  2. "Attempts to use other thread-based services such as the AWT event-dispatch thread, for example, may lead to deadlocks"

    Swing runs on-top of AWT, whose underlying event-dispatch thread may be in the process of shutting down, too. Trying to use Swing or AWT while shutting down can lead not only to dead locks but also may just not work at all, anyways.

  3. "If the virtual machine aborts then no guarantee can be made about whether or not any shutdown hooks will be run"

    There are no guarantees your user could even possibly get your message, since shutdown hooks are only guaranteed to run when it exits normally or terminated -- not when halted or aborted.

obataku
  • 29,212
  • 3
  • 44
  • 57
4

Shutdown hooks are supposed to execute as quickly as possible. That does not include waiting for a user to confirm a dialog. In any case you have no guarantee that the Swing event thread is still running.

You can't do this.

user207421
  • 305,947
  • 44
  • 307
  • 483
2
  1. Swing GUI must be done on Event Dispatch Thread, then

  2. Simple way, but required end user action (close JDialog)

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • I'm afraid I don't see what relevance your answer has to the original question. – Duncan Jones Aug 25 '12 at 07:32
  • @DuncanJones He's answering it. His answer is about shutdown hook + links to a possible solution... – Alex Coleman Aug 25 '12 at 07:33
  • @mKorbel I don't see how the first option would work; I'm not calling for the shutdown hook, it's happening as a random occurence, I just want to handle it with a popup window. For the second, I see some potential in that, however, will Runtime#exec work when the JVM/computer is shutting down? – Alex Coleman Aug 25 '12 at 07:35
  • @Alex Coleman `Runtime#exec` is one of seven options, if is there something that you don't understand, then search for question about JVM, Swing and why Shutdown Hook doesn't works for Swing GUI with background taks – mKorbel Aug 25 '12 at 07:47
2

If there is, it won't help you.

The shutdown hooks are invoked asynchronously as part of the JVM shutdown, so a "confirm" dialog won't really confirm anything as you can't halt or reverse the shutdown process. Waiting for a user to make a decision is not the kind of action a shutdown hook is meant for. A shutdown hook in an interactive program does not make sense. The real use case for shutdown hooks is:

for releasing resources and other housekeeping when the JVM shutsdown

It is also important to note the shut down hook wont always be run, for more see my answer here: How to shutdown java application correctly from C# one

Community
  • 1
  • 1
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • Even though it's handled asynchronously, the thread is still not daemon and thus it might still work... not that you should __ever__ do it. – obataku Aug 25 '12 at 08:08
  • @veer true but I guess the biggest problem the OP will face is that not all terminations of an app will invoke the shutdown hook – David Kroukamp Aug 25 '12 at 08:13
-1

I'm not sure about your question, but I think its impossible to run or display a popup window when JVM is shutting down. Its like your trying to run while preparing to sleep? Just guessing. :)

user1577161
  • 165
  • 1
  • 4
  • 18