Hello forums!
I've opened this thread to discusss "self deletion" in Java.
It is perfectly possible, but has a trick to it. Obviously, you can't delete a file that is running. We'd have to send a scheduled delete request to the shell of the OS. (though its bad architecture since you're program is dependant on an specific platform) anyways that can easily be forseen.
-from another forumAnything that is being run cannot be deleted as it is a process in use. What you could do (and this is dirty, bad architecture) is use Runtime.exec() to (on windows) use the "AT" command to schedule a delete command on your java code files. Again, this is not the best idea and it's platform dependant (so you'd have to have a way to do this on linux too - cron would do it).
I'd say to use the ProcessBuilder instead of the Runtime.exec(), because it is very limmited.
Some people are using this
The reason why I've started to look at this option is because, the program I am working with, generates 2 copies of itself, and has one of the deletes the original, but it generates several unsyncronized problems, which thread and waiting would not prevent it to a 100%. So I rather have each program take care of itself, sending a delete request on itself upon conclusion of its instructions.public static void main(String[] args) throws Throwable {
File f = new File("DeleteMe.jar");
f.deleteOnExit();
}
Ideas, advice, comment?
I appreciate your feedback. Meanwhile.. I'd be google-ing and trying to apply it.