Question

Is it possible to schedule a executable file to run just before log-off using java? I am working on a application that needed to send a message to the sever just before the system log-off/shutdown. But I didn't find any method to solve it. If anyone know plz help me.

Thanks in advnce.

Was it helpful?

Solution

As stated in the comments a ShutdownHook is probably what you are searching for. Short example:

Runtime.getRuntime().addShutdownHook(new Thread() {
  @Override 
  public void run() {
    // Code to be executed when JVM exits
    System.out.println("JVM exit");
  }
});

Note that the ShutdownHook will execute whenever the JVM exits. So it will also execute when your program terminates normally.

If you have any further questions comment on this answer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top