Dylan Wheeler
Dylan Wheeler

Reputation: 7074

Java Program Deletion (Mac/Linux)

How would I make my Java application delete itself and then close out of itself on a Mac/Linux OS? I have tried a couple Runtime commands, but none of them seem to work.

Something like this maybe:

try {
    Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", "rm \"" + getRunningJarPath() + "\""});
} catch (final IOException e) {}

Note: The method: getRunningJarPath IS accurate.

Upvotes: 1

Views: 193

Answers (3)

You do not state why you want the program to be deleted afterwards.

Anyway, you should not rely on manipulating the file system yourself. I would suggest you look into Java WebStart instead as it provides all the file system caching (including expiration) you need.

Upvotes: 0

alf
alf

Reputation: 8513

Generally, it's not possible: you program may happen not to have rights to remove itself.

Additionally, there's a problem: while your program is running, JVM holds a read lock on the JAR, so it well may be that you cannot delete it right away.

Usually, you run your program from a script which would check the exit code and remove the files if necessary.

Upvotes: 1

nidhin
nidhin

Reputation: 6910

You can create a cron job to delete file, that will start after a delay

Upvotes: 0

Related Questions