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

  • Home
  • SEARCH
  • 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 6176161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:03:03+00:00 2026-05-24T00:03:03+00:00

i wanna create a java application that must perform some essential action (closing of

  • 0

i wanna create a java application that must perform some essential action (closing of file objects successfully or any other task) before it is terminated by the user using task manager or before the system is logged off (or shut down) by the user.

Is it possible in java????

Thanks in advance….

  • 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-24T00:03:03+00:00Added an answer on May 24, 2026 at 12:03 am

    You can implant shutdown hook in JVM – see this example: http://www.crazysquirrel.com/computing/java/basics/java-shutdown-hooks.jspx. Though it may not work in some cases like system crash, someone pulling the server plug etc. 🙂

    ========================

    Update

    Here is relevant extract from Runtime API about your scenarios:

    => Logoff and shutdown should trigger the hook properly

    The Java virtual machine shuts down in response to two kinds of
    events:

    The program exits normally, when the last non-daemon thread exits or when the exit (equivalently, System.exit) method is invoked, or
    
    The virtual machine is terminated in response to a user interrupt, such as typing ^C, or a system-wide event, such as user logoff or system shutdown.
    

    => Task Manager may not – and that’s why you are not seeing your print statement

    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.

    ===================================================================

    I made the following changes to that example code and it works:

    1. Placed a fake pause to keep JVM alive long enough for you to trigger Windows logoff
    2. Created a file on C drive (make change accordingly) so I can inspect the result when I log back in

    Try it out…

    package org.helios.util;
    
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    
    public class ShutdownHook {
    
        public static void main(String[] args) {
            Hook hook = new Hook();
            System.out.println("Running Main Application...");
            Runtime.getRuntime().addShutdownHook(hook);
            for (int i = 0; i < 50; i++) {
                //Pause for 4 seconds
                try {
                    Thread.sleep(4000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Going back to sleep");
            }
            System.out.println("Normal Exit...");
        }
    
        private static class Hook extends Thread {
            public void run() {
                try {
                    FileWriter fstream = new FileWriter("c:\\out.txt");
                    BufferedWriter out = new BufferedWriter(fstream);
                    out.write("JVM Shutting down");
                    out.close();
                } catch (Exception e) {
                    System.err.println("Error: " + e.getMessage());
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanna create an application in android to install a Hindi Font. Can some
i wanna create a simple bookmarklet that hides all images from the current website
I wanna create Contextual Tab using Webpart In Sandbox Solution..that time I am getting
I have create several canvas with transparent background and wanna make some move event
I wanna create queue management system (for bank, hostpital...) Something like that There will
What I wanna create is page that fetches results from my DB and display
I want the asp application to create a folder that has access only to
I wanna create an anonymous type that I can set the property name dynamically.
I wanna create some loading dots, like this: At 0000 miliseconds the span content
I wanna create a global object in cpp program, how do I do that?

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.