12

I have added the following code to my program:

Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
    @Override
    public void run() {
        System.out.println("exit");
    }
}){});

I however do not see the message. Additional information: I am running the program from inside the Netbeans IDE on Java 7.

EDIT: I forgot to add that there is a global Thread that keeps the program alive. I close it by pressing the [x] in Netbeans lower right corner.

hoijui
  • 3,615
  • 2
  • 33
  • 41
skiwi
  • 66,971
  • 31
  • 131
  • 216
  • Try sending a `SIGHUP` to the Java process (`kill -1 `) instead of clicking a UI button. http://en.wikipedia.org/wiki/Unix_signal#POSIX_signals – Matt Ball Oct 28 '13 at 15:55
  • Your example works fine for me when I paste it into a `main` method. – pburka Oct 28 '13 at 15:59
  • 4
    Note that the `{}` is unnecessary. It actually creates an anonymous inner subclass of `Thread`. – pburka Oct 28 '13 at 16:01
  • The Runnable is not necessary. Instead, you can override the run method of the Thread and it will be functionally identical. – H2ONaCl Oct 06 '16 at 22:30

2 Answers2

18

The JVM can shutdown in either an orderly or abrupt manner. A shutdown hook runs for an orderly shutdown: when the last normal thread terminates, someone calls System.exit or by other platform specific means (such as typing Ctrl-C).

Shutdown hooks will not run for an abrupt shutdown of the JVM. As you are pressing the [x] in Netbeans lower right corner, this will cause an abrupt shutdown of the JVM and this is why the shutdown hook was not started.

For example :

public class ShutdownHook {
public void attachShutDownHook() {
    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            System.out.println("exit");
        }
    });

}

public static void main(String[] args) {
    ShutdownHook sample = new ShutdownHook();
    sample.attachShutDownHook();
    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

If you run the above code, and let the program complete normally, you will see exit printed on the console. But if you press [x] (within 3 secs) to close it abruptly, the shutdown hook will not run and there will not be any exit printed on the console.

Debojit Saikia
  • 10,532
  • 3
  • 35
  • 46
  • Okay, then I understand that. The current code (with a meaningful shutdown hook) is still worth keeping in. But is there also a way to do a proper shutdown with het Netbeans close? Also what would be a nice way of shutting down the program normally? I do not have access to System.In, as the console is being used for output constantly. – skiwi Oct 28 '13 at 16:32
  • you can use `System.exit(0);` in code to shutdown your program. – Debojit Saikia Oct 28 '13 at 16:52
  • The program is a program that must react when files are being added to a specific folder, so I'm afraid it needs to be running forever. Still need some way to properly shut it down in case of maintenance though. – skiwi Oct 28 '13 at 17:07
  • 1
    when you type crtl+c on console, the shutdown hook will run. and if have a UI, you can use `System.exit(0);`. – Debojit Saikia Oct 29 '13 at 02:59
2

I forgot to add that there is a global Thread that keeps the program alive. I close it by pressing the [x] in Netbeans lower right corner.

Well this is it, closing program by "x" in netbeans lower right corner is not regular shut down, it just breaks everything and shut it down.

ShutdownHook works only when the program regulary exits...

Zong
  • 6,160
  • 5
  • 32
  • 46
libik
  • 22,239
  • 9
  • 44
  • 87