Deleting a directory on exit in Java

directory, java

Solution

You could try this:

Runtime.getRuntime().addShutdownHook(new Thread() {

      @Override
      public void run() {
        /* Delete your file here. */
      }
 });

A lot depends on how your program is ending.

Problem

I was just wondering if it was possible to delete a directory when the application was closed? Is there an easy way to do something when the application is closed? Currently my app downloads the class files at runtime so what I'm really looking for is something to remove them from the clients system. I've tried so far ``` File directory = new File("macros/bin/"); directory.deleteOnExit(); ``` As well as a delete on runtime of the downloaded startup files (which obviously can't be done seeing as it needs them in order to run).

Original source