views:

2538

answers:

6

Hi all.

I have a shutdown hook in my application (created using Runtime.getRuntime().addShutdownHook). However if I launch the application from within Eclipse, when it is shut-down the shutdown hook doesn't execute.

I think this is because Eclipse sends the equivalent of a force-kill signal to the process, which doesn't cause the shut-down hook to execute (equivalent of taskkill /F on Windows or kill -p on linux), though I'm not absolutely sure.

Does anyone know how to get around this? I'm running Windows (Vista), and I've a feeling it may be a Windows-specific issue, but I'm not sure (I read something about it a while back, but can't remember anything, only that nobody suggested a solution).

Thanks.

Neil.

A: 

I'm not sure how to fix this but IntelliJ added a separate button to their 'Run' dialog which will shutdown the VM in a way that calls the Shutdown Hooks. Their debugger does not have this feature.

Javamann
A: 

Hmmm, "Use Intellij" isn't really the answer I was looking for!

From the comments:

Don't run your app from within Eclipse?

Also, let's assume I'm not an idiot and I've thought of that already.

A: 

Eclipse is open-source. You could add the button a la IDEA to the codebase yourself. Also, probably not the answer you were looking for!

oxbow_lakes
Hmmm, not a bad idea. If I knew how Intellij managed to do this I wouldn't mind writing my own version.
Can't you just get hold of the java.lang.Process and call destroy()?
oxbow_lakes
+4  A: 

First off, is your application ending or are you forcibly ending it? If you are forcibly ending it (via the stop button), this Eclipse bug report has details about why this might not be working.

Failing that, you may be correct about this being Windows-specific behavior. I'm on a Mac so I can't confirm, sorry. However, I can tell you that the following test program does execute the shutdown hooks as expected.

public class MyShutdownHook
{
    public static void main( String[] args )
    {
     System.out.println( "Entering main." );

     Runtime.getRuntime().addShutdownHook( 
      new Thread(
       new Runnable() {
        public void run() {
         System.out.println( "Shutdown hook ran." );
        } 
       }
      )
     );

     System.out.println( "Exiting main." );
    }
}

The Javadocs for Runtime#addShutdownHook do mention that shutdown hooks do not run when the JVM is aborted, not exited normally, so again, you are probably correct in your assumptions. That being said, here are some things to try. Again, sorry I can't confirm these ahead of time -- no Windows here. (Blessedly!)

  • Make sure the JVM does not include the -Xrs option. This has a side-effect on shutdown hooks.
  • Try launching the JVM using either the -server or -client option. It's been my experience that edge-case behavior can be effected by the choice of VM mode. (Especially with regard to threading -- at least on Mac.)
  • Are you launching your app under a profiler or the like? Loading any native libraries?
  • Are you getting some fatal error and missing it? E.g., OutOfMemoryError or the like?
  • Try checking/unchecking the Launch in background option from your application's Run Configuration dialog in Eclipse.
  • Last but not least: Invoke System.exit() manually if you have the opportunity. :)
JLR
A: 

I'm stuck in websphere at the moment, and don't see what I'm looking for. But I do remember eclipse having a run configuration option related to launching the application in the same VM. Is it possible your launching your java application in the same VM as the eclipse VM?

But the option slips my mind.

+2  A: 

I faced this problem and used the following hack at the end of my main method to get around it

 if (Boolean.parseBoolean(System.getenv("RUNNING_IN_ECLIPSE")) == true) {
      System.out.println("You're using Eclipse; click in this console and " +
          "press ENTER to call System.exit() and run the shutdown routine.");
      try {
       System.in.read();
      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
      System.exit(0);
     }

HTH, Sairam