Search code examples
javanetbeanssignalsnetbeans-7shutdown-hook

How netbeans stops a run?


I'm programming with Java in Linux using Netbeans 7 and as my program (sometimes) could not exit (not in this eon, maybe) I create a thread to handle shutdown

Runtime.getRuntime().addShutdownHook(new StopThread());

But when I launch the code with netbeans (F6) and stop it through the "STOP" button the thread is not created; but if I run the program through the terminal and send CTRL-c the thread is created.
The question is: what type of signal netbeans launches to terminate the program?
Or (better): how can i handle the stop of netbeans such that the thread is created if i force to stop the program?
Or is there a way to modify how the stop works?

Thank you!


Solution

  • You are on Linux and this makes it really easy. I have written very little test and observe the same behavior as you - with crtl+c running from the terminal hook is working, stopping in Netbeans it does not. Here are some workarounds:

    1. I'm on Gnome and when I find the running process in the System Monitor and press "End process" on it - hook is working.
    2. Other, more universal way:

      $ ps ax | grep Hook
      17144 ?        Sl     1:21 /usr/lib/jvm/java-6-sun/bin/java -Dfile.encoding=UTF-8 -classpath /home/isopov/NetBeansProjects/ShutdownHookTest/build/classes:/home/isopov/NetBeansProjects/ShutdownHookTest/src shutdownhooktest.ShutdownHookTest
      17176 pts/2    R+     0:00 grep --color=auto Hook
      $ kill -15 17144
      

    Or in one line:

        $ ps x | grep HookTest | grep java | awk '{print $1}' | xargs kill -15