Search code examples
javaauto-updateshutdown-hook

In a Java application, is it a good idea to use a shutdown hook to auto-update the app?


The idea is to check for updates while the application is running. The update consists of one main jar and a couple of third party jars. If updates are available:

  • Download them
  • Put them in a temp directory
  • Add a shutdown hook that replaces all the actual jars with the jars from the temp directory

Is this a good idea?


Solution

  • No. From the Javadoc:

    Shutdown hooks should also finish their work quickly. When a program invokes exit the expectation is that the virtual machine will promptly shut down and exit. When the virtual machine is terminated due to user logoff or system shutdown the underlying operating system may only allow a fixed amount of time in which to shut down and exit. It is therefore inadvisable to attempt any user interaction or to perform a long-running computation in a shutdown hook.

    Ergo you should not engage in long-running or blocking activities in a shutdown hook.