-2

I want to write the system shutdown time to a txt file. I am using the shutdownhook thread.

I have written the file writing mechanism in the run method of the thread. But it is not working.. This is my code..

public class JVMShutdownHookTest {
  public static void main(String[] args) {
    JVMShutdownHook jvmShutdownHook = new JVMShutdownHook();
    Runtime.getRuntime().addShutdownHook(jvmShutdownHook);
    System.out.println("JVM Shutdown Hook Registered.");
    System.out.println("Pre exit.");


  }

  private static class JVMShutdownHook extends Thread {
    public void run() {
      System.out.println("JVM Shutdown Hook: Thread initiated.");

        File file = new File("C:\\Users\\karthi\\Desktop\\Shutdown.txt");


        try {
            //FileWriter fw = new FileWriter(file, true);
            PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));
            pw.println("Shutdown Time is ======= " + Calendar.getInstance().getTime());
            pw.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
  }
}
Karthik
  • 53
  • 2
  • 3
  • 10
  • 4
    How is it not working? What behavior are you expecting? – Reimeus May 20 '13 at 13:52
  • there could be some exception occurring due to FileIO and you are missing it since it is written to stderr. Add a logger and check if there are exceptions that occur. – NiranjanBhat May 20 '13 at 13:54
  • Have you tried by adding manually `System.exit(0)`? – Mik378 May 20 '13 at 13:55
  • @NiranjanBhat When I execute these lines, file created at that time itself. But my requirement is , when I shutdown the system, the file should generated with that time – Karthik May 20 '13 at 13:57
  • @Karthik have you tried my answer? Got different results? – mauretto May 20 '13 at 14:31

5 Answers5

2

You should generally refrain from using shutdown hooks for any useful purposes: they are there only as a last-resort facility to clean up, and definitely not acquire any new resources, such as opening files. There is absolutely no guarantee that such code will ever succeed in a shutdown hook.

You'll have to devise an explicit mechanism that will execute your code at shutdown.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • Is there any way in java to capture the shutdown time? Like, when I shutdown the system, a .txt file should be generated with the system time.. – Karthik May 20 '13 at 13:59
0

The code looks correct but you might have problems with the file writing functionality

  • Launch the thread yourself and see if the file is created and debug from there
  • Also close the pw variable in a final block rather in the try block
toomasr
  • 4,731
  • 2
  • 33
  • 36
0

Most systems (that I know of) keep a log of when the system is shut down. Do you absolutely NEED to use Java for this, and do you absolutely NEED to get the time independently? If all you need is system shutdown times, it may be easier (and cleaner) to read the system log.

JohnFilleau
  • 4,045
  • 1
  • 15
  • 22
0

What do you mean that it's not working? I tried and it works fine on my machine.

Did you get some error, the file is not created or are its contents different than you expected?

mzywiol
  • 386
  • 1
  • 3
  • 11
-1

I guess the problem is that your class will end to execute before to intercept the Shutdown... Try to add an infinite loop or a very long Thread.sleep() at the end of your main method and retry. In your case the jvm will shoutdown immediatly and you'll have your execution end time!

mauretto
  • 3,183
  • 3
  • 27
  • 28