Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7492929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:39:59+00:00 2026-05-29T16:39:59+00:00

I have a bat script which runs a java application. If I press ctrl+c

  • 0

I have a bat script which runs a java application. If I press ctrl+c on it, it the application terminates gracefully, invoking all the shutdown hooks. However, if I just close the cmd window of the bat script, the shutdown hooks are never invoked.

Is there a way to solve this? Perhaps there’s a way to tell the bat script how to terminate the invoked applications when its window is closed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T16:40:03+00:00Added an answer on May 29, 2026 at 4:40 pm

    From addShutdownHook documentation:

    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.

    So i think nothing to do here, unfortunately.


    CTRL-CLOSE signal in Windows Console. Seems non-tweakable.

    Quoting above link:

    The system generates a CTRL+CLOSE signal when the user closes a console. All processes attached to the console receive the signal, giving each process an opportunity to clean up before termination. When a process receives this signal, the handler function can take one of the following actions after performing any cleanup operations:

    • Call ExitProcess to terminate the process.
    • Return FALSE. If none of the registered handler functions returns TRUE, the default handler terminates the process.
    • Return TRUE. In this case, no other handler functions are called, and a pop-up dialog box asks the user whether to terminate the process. If the user chooses not to terminate the process, the system does not close the console until the process finally terminates.

    UPD. If native tweaks are acceptable for you, WinAPI SetConsoleCtrlHandler function opens way for suppressing of default behavior.

    UPD2. Revelations on Java signal handling and termination relatively old article, but section Writing Java signal handlers really may contain what you need.


    UPD3.
    I’ve tried Java signal handlers from article above. It works with SIGINT nicely, but it not what we need, and i decided to carry it with SetConsoleCtrlHandler. The result is a bit complicated and may be not worth to implement in your project. Anyway, it could help someone else.

    So, the idea was:

    1. Keep reference to shutdown handler thread.
    2. Set custom native console handler routine with JNI.
    3. Call custom Java method on CTRL+CLOSE signal.
    4. Call shutdown handler from that method.

    Java code:

    public class TestConsoleHandler {
    
        private static Thread hook;
    
        public static void main(String[] args) {
            System.out.println("Start");
            hook = new ShutdownHook();
            Runtime.getRuntime().addShutdownHook(hook);
            replaceConsoleHandler(); // actually not "replace" but "add"
    
            try {
                Thread.sleep(10000); // You have 10 seconds to close console
            } catch (InterruptedException e) {}
        }
    
        public static void shutdown() {
            hook.run();
        }
    
        private static native void replaceConsoleHandler();
    
        static {
            System.loadLibrary("TestConsoleHandler");
        }
    }
    
    class ShutdownHook extends Thread {
        public void run() {
            try {
                // do some visible work
                new File("d:/shutdown.mark").createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("Shutdown");
        }
    }
    

    Native replaceConsoleHandler:

    JNIEXPORT void JNICALL Java_TestConsoleHandler_replaceConsoleHandler(JNIEnv *env, jclass clazz) {
        env->GetJavaVM(&jvm);
        SetConsoleCtrlHandler(&HandlerRoutine, TRUE);
    }
    

    And handler itself:

    BOOL WINAPI HandlerRoutine(__in DWORD dwCtrlType) {
        if (dwCtrlType == CTRL_CLOSE_EVENT) {
            JNIEnv *env;
            jint res =  jvm->AttachCurrentThread((void **)(&env), &env);
            jclass cls = env->FindClass("TestConsoleHandler");
            jmethodID mid = env->GetStaticMethodID(cls, "shutdown", "()V");
            env->CallStaticVoidMethod(cls, mid);
            jvm->DetachCurrentThread();
            return TRUE;
        }
        return FALSE;
    }
    

    And it works. In JNI code all error checks are omitted for clearance. Shutdown handler creates empty file "d:\shutdown.mark" to indicate correct shutdown.

    Complete sources with compiled test binaries here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this .bat script which I use to maven package my application. Problem
I have a bat script in which gets all files in a folder and
I have a script which detects OS using Catalina.bat for windows and Catalina.sh for
I have a setup script which I run in the morning which starts all
I have a deployment script (.bat), part of which requires calling other programs and
I have a bat script which has block1 commands. I call the block1 like
I have a bat script which at one point redirects the stderr of a
I have a Windows batch script (my.bat) which has the following line: DTBookMonitor.exe 2>&1
I have a simple bat script that copies files from an known directory to
I'm writing a bat script and I have to save the date from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.