Shutdown hook doesn't work in Eclipse [duplicate]
Asked Answered
M

6

27

I have added a shutdown hook via:

Runtime.getRuntime().addShutdownHook(myShutdownHook);

It works fine normally, but not when I click the red stop button in Eclipse. Is there a way to make the shutdown hook be called in Eclipse?

Metamathematics answered 11/10, 2012 at 9:37 Comment(2)
the questions is marked duplicate to a question that was asked 2 years laterMetamathematics
actually the other one was two years prior (09 instead of 11). You probably misread the day (13) for the year.Gadabout
C
17

The red stop button forcibly kills the application, i.e. not gracefully, so the JVM doesn't know that the application is exiting, therefore the shutdown hooks are not invoked.

Unfortunately, there is no way (in Windows, at least) to provide a mechanism that ensures that the hook is always invoked. It's just something that may be invoked, but there is no guarantee.

Cassino answered 11/10, 2012 at 9:42 Comment(3)
Please quote a source citing "there is no way to provide a mechanism that ensures that the hook is always invoked", Because the page docs.oracle.com/javase/7/docs/api/java/lang/… clearly states that hooks will run when "the program exits normally", or "the virtual machine is terminated in response to a user/system interrupt".Gaddi
@Gaddi That statement was based on a test I did where I closed a console window which was running a batch script invoking a Java application. When closing the window, the shutdown hook was not invoked.Insurgent
This, and stopping the application from Eclipse, are examples of the program not exiting normally.Insurgent
B
9

I made a hack by replacing JavaProcess with decorated one:

    IProcess p = launch.getProcesses()[0];
    launch.addProcess(new JavaProcessDecorator(p));
    launch.removeProcess(p);

And decorator is overriding terminate function.

public class JavaProcessDecorator implements IProcess {

private IProcess p;

public JavaProcessDecorator(IProcess p) {
    this.p = p;
}

private boolean sigkill = false;

@SuppressWarnings("rawtypes")
@Override public Object        getAdapter(Class arg)                { return p.getAdapter(arg); }
...
@Override public ILaunch       getLaunch()                          { return p.getLaunch(); }
@Override public IStreamsProxy getStreamsProxy()                    { return p.getStreamsProxy(); }
@Override public void          setAttribute(String s1, String s2)   {        p.setAttribute(s1, s2); }
@Override public void          terminate() throws DebugException    {
    if(!sigkill) {
        try {
            IDebugIService cs = DirmiServer.INSTANCE.getRemote("main", IDebugIService.class);
            if(cs != null) cs.modelEvent(new TerminateRequest());
        } catch (RemoteException e) { }
        this.sigkill = true;
    } else p.terminate();
}}

At first click on red button, I send a message to application asking for gently termination. If it is not working, second click on red button will kill it.

Burg answered 13/5, 2015 at 18:35 Comment(0)
G
2

I know I'm a bit late to the party, but I found this thread seeking for help and so will probably others.

We had the same issue and solved it with an Eclipse plugin (on Linux) which provides additional stop buttons now. I hope this serves all of you as well as it did help us :)

Gadabout answered 20/11, 2017 at 14:47 Comment(1)
Can be installed easily via Eclipse Marketplace from the Help menu (the link contains instructions to download and install manually, I got some errors with that)Blowing
F
0

Red stop button just terminates the application and according to eclipse devs they can't do anything about it see this issue in eclipse bug tracker.

Forlorn answered 3/7, 2014 at 6:42 Comment(0)
B
0

@Pacerier - From the Javadoc: 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.

Batter answered 24/7, 2014 at 21:30 Comment(0)
C
0

If you just want to test if the hook is working or not, throw new RuntimeException() from the trigger point. This should call the shutdown hook even from Eclipse.

Crites answered 11/8, 2016 at 1:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.