By Neil

2009-02-13 18:42:25 8 Comments

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.


@basin 2017-08-18 10:42:57

On Windows to gracefully stop a java application in a standard way you need to send Ctrl + C to it. This only works with console apps, but Eclipse uses javaw.exe instead of java.exe. To solve this open the launch configuration, JRE tab and select "Alternative JRE:". The "Java executable" group box appears and allows to enter the alternate executable "java".

Now we need an external program to send Ctrl-C to a process with a hidden console. I found hints here and here. Our program attaches to the console of the desired process and sends the console event.

#include <stdio.h>
#include <windows.h>

int main(int argc, char* argv[])
    if (argc == 2) {
        unsigned pid = 0;
        if (sscanf_s(argv[1], "%u", &pid) == 1) {
            FreeConsole(); // AttachConsole will fail if we don't detach from current console
            if (AttachConsole(pid)) {
                //Disable Ctrl-C handling for our program
                SetConsoleCtrlHandler(NULL, TRUE);
                GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0);
                return 0;

    return 1;

Test java program:

public class Shuthook {

    public static void main(final String[] args) throws Exception {
        Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run() {
                System.out.println("Shutting down...");

        String sPid = ManagementFactory.getRuntimeMXBean().getName();
        sPid = sPid.substring(0, sPid.indexOf('@'));

        System.out.println("pid: " + sPid);


Terminating it:

C:\>killsoft.exe 10520

Test program output in Eclipse:

pid: 10520
Shutting down...

@Sairam R 2009-03-11 23:37:05

I used the following hack at the end of my main method to get around the problem:

if (Boolean.parseBoolean(System.getenv("RUNNING_IN_ECLIPSE"))) {
    System.out.println("You're using Eclipse; click in this console and " +
            "press ENTER to call System.exit() and run the shutdown routine.");
    try {;
    } catch (IOException e) {

@mr_georg 2012-05-11 08:22:29

Note: You must manually add the environment variable "RUNNING_IN_ECLIPSE" in the run configuration, and set the value to TRUE.

@Mubashar 2012-10-10 03:04:15

Or you can just do lines without if/else for the purpose of debugging only.

@Pacerier 2014-07-19 15:47:51

This solution doesn't work if we want to test "shutdown" on a console app that is running halfway.

@Shashank 2015-09-25 08:37:06

Sairam is right here, By calling System.exit(0) we can terminate eclipse JVM and can see Shutdown hook results

@Jason Axelson 2012-03-23 01:35:17

Here's a script that you can run outside of eclipse to list the available processes running under Eclipse that you could kill.

set -o nounset                              # Treat unset variables as an error

PROCESSES=$(ps axo pid,ppid,command)

# Find eclipse launcher PID
LAUNCHER_PID=$(echo "$PROCESSES" | grep "/usr/lib/eclipse/eclipse" |grep -v "launcher"|awk '{print $1}')
echo "Launcher PID $LAUNCHER_PID"

# Find eclipse PID
ECLIPSE_PID=$(echo "$PROCESSES" | egrep "[[:digit:]]* $LAUNCHER_PID " | awk '{print $1}')
echo "Eclipse PID $ECLIPSE_PID"

# Find running eclipse sub-process PIDs
SUB_PROCESS=$(echo "$PROCESSES" | egrep "[[:digit:]]* $ECLIPSE_PID " | awk '{print $1}')

# List processes
    DRIVER=$(ps --no-headers o pid,ppid,command $PROCESS | awk '{print $NF}')
    echo "$PROCESS $DRIVER"

echo "Kill a process using: 'kill -SIGTERM \$PID'"

@Pacerier 2014-07-19 15:48:49

Hmm, but killing it is the same as forcibly stopping it right? As such, the shutdown hooks wouldn't run.

@Jason Axelson 2014-07-19 19:46:47

@Pacerier depends on what signal you use. The default is TERM which will run the shutdown hooks. kill -9 will not run the shutdown hooks. More info:…

@guyumu 2009-02-18 06:29:38

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.

@JLR 2009-02-18 03:24:33

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." );

            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. :)

@Pacerier 2014-07-19 15:48:16

How did you know to search for the -Xrs option?

@Javamann 2009-02-13 19:07:31

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.

Related Questions

Sponsored Content

41 Answered Questions

[SOLVED] How can you speed up Eclipse?

  • 2008-11-25 03:18:56
  • user2427
  • 608290 View
  • 1277 Score
  • 41 Answer
  • Tags:   eclipse performance

31 Answered Questions

19 Answered Questions

[SOLVED] How to duplicate a whole line in Vim?

49 Answered Questions

[SOLVED] Does a finally block always get executed in Java?

21 Answered Questions

[SOLVED] How do I call one constructor from another in Java?

  • 2008-11-12 20:10:19
  • ashokgelal
  • 873028 View
  • 2275 Score
  • 21 Answer
  • Tags:   java constructor

9 Answered Questions

27 Answered Questions

[SOLVED] How to get an enum value from a string value in Java?

  • 2009-03-02 22:56:34
  • Malachi
  • 1140831 View
  • 2000 Score
  • 27 Answer
  • Tags:   java enums

1 Answered Questions

[SOLVED] How do I kill a Java application on Linux so that the shutdown hook is executed?

  • 2017-02-19 15:38:25
  • Dmitrii Pisarenko
  • 173 View
  • 2 Score
  • 1 Answer
  • Tags:   java linux ubuntu

2 Answered Questions

[SOLVED] Useful example of a shutdown hook in Java?

  • 2010-05-27 14:20:58
  • Jason S
  • 136986 View
  • 125 Score
  • 2 Answer
  • Tags:   java shutdown-hook

Sponsored Content