3

I'm trying to make a simple Java wordcounting program.

When it reads Microsoft Office files, it first reads the text of the XML files (Microsoft Office files are actually bundles of zipped xml files!) and then reads them to a folder called "converted".

I want "converted" to be deleted right after the program ends, so I added a

new File("converted").deleteOnExit();

which does that well.

However, if the user of the program presses Ctrl+C in the command prompt, then the program will exit early, and the folder will not be deleted.

I would like to know if there's a way to throw an exception if a program is exited. It seems unlikely, because a forced exit of a program will probably stop any code, but I was wondering if this is possible. Then, I'll be able to handle the exception and exit the program correctly, so that the directory will be deleted. I mean, I can add this:

catch(ExitException e) {    // if the exception is called "ExitException"
    System.err.format("Program ended unexpectedly.%n");
    System.exit(-1);        // this line so that the folder can delete
}

(The way I understand it, the folder is only deleted if a System.exit() is called. Correct me if I'm wrong.)

Thanks.

Jonathan Lam
  • 16,831
  • 17
  • 68
  • 94
  • 3
    You need shutdown hook, check this answer http://stackoverflow.com/a/2922031/1516873 – user1516873 Nov 26 '14 at 16:22
  • also note, this can't handle sigkill (kill -9) – user1516873 Nov 26 '14 at 16:26
  • @user1516873 what's "sigkill (kill -9)"? Sorry, I'm a beginner programmer. – Jonathan Lam Nov 26 '14 at 16:27
  • @Jon It's a unix thing which basically closes the program without the program getting the option to cancel it. You can't really do anything about it, but you shouldn't need to worry about it as it is really only used when a program is completely borked. – Pokechu22 Nov 26 '14 at 16:31
  • never mind, it is a linux [command](https://en.wikipedia.org/wiki/Kill_%28command%29). Kill -9 is a way to terminate hanged process. – user1516873 Nov 26 '14 at 16:32

1 Answers1

1

There isn't really a way to have it throw an exception on ^C (Control C), but you can have code run when the program is exited in any way as seen below.

Try using shutdown hooks: Runtime.getRuntime().addShutdownHook() should run even on ^C.

Note that this won't run under very specific cases as defined there (SIGKILL), but it will handle most lower things.

Try this instead (run it once at some point in your program):

Runtime.getRuntime().addShutdownHook(new Thread() {
    public void run() {
        try {
            new File("converted").delete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});

And get rid of your new File("converted").deleteOnExit();.

Pokechu22
  • 4,984
  • 9
  • 37
  • 62
  • how come I can run it at any point in my program? Is there a recommended spot where I should put it? – Jonathan Lam Nov 26 '14 at 16:47
  • @Jon You can put that at any point because it just says "Do this later"; it doesn't actually do it. (You could do the same with the `file.deleteOnExit` code). I recommend putting it near the start. – Pokechu22 Nov 26 '14 at 16:50