james
james

Reputation: 91

How to delete the Executable Jar File by it self?

I developed a simple java application ,is it possiblefor the application (Executable Jar File) to find its current path and delete it self from both the current place and from Recycle Bin after a certain time.

Upvotes: 1

Views: 8922

Answers (4)

regev avraham
regev avraham

Reputation: 1112

In order to delete the jar file, I recommend:

  • Creating a bat/sh file
  • Run the file
  • Close the jar file with System.exit method

Within the bat/sh file:

  • Loop until success deletion
  • Delete the bat/sh file within itself(unlike the jar file bat/sh file can delete itself)

Upvotes: 0

Neeraj Jain
Neeraj Jain

Reputation: 7720

Windows strictly restricts you from doing this untill the jar is in use

It is something like this :

Close Before Unstalling

In Linux you can do it here is How to delete a executing jar file

Upvotes: 0

djsumdog
djsumdog

Reputation: 2710

There are already questions/answers that show you how to get the currently running jar file. Keep in mind, the methods aren't consistent across platforms:

How to get the path of a running JAR file?

Deleting a file in Java is fairly straight forward as well:

import java.io.File;
...
new File("c:\\path\\to\\whatever.jar").delete();

On an operating system that doesn't have file locking, you can simply delete the jar you're running from as it's already loaded into memory. On operating systems that lock files, this may not be possible if the JVM decides to lock the currently executing jar(s).

Upvotes: 0

JuniorCompressor
JuniorCompressor

Reputation: 20025

No, when java runtime starts and uses this jar file, windows prevents it from being deleted. In other operating systems like Linux you can delete files even if they are used.

Upvotes: 2

Related Questions