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.
-
Need to write another jar to delete that jar... Normally if you are writing a virus you would do that to prevent people from finding your tracks because a program that deletes a file as incriminating. Most of the time in windows we use the windows installation service to handle such uninstalls... – Ya Wang Feb 09 '15 at 20:39
-
1@JohnVint I have no idea what his intention is maybe he is trying to auto update his application. In which case he deserves to know how to do it properly. – Ya Wang Feb 09 '15 at 20:41
-
@james Then what is it? You had 10 minutes to figure out good explanation for John Vint comment :) – Pshemo Feb 09 '15 at 20:44
-
Do you think it's the only thing left to complete a super duper virus? :p – JuniorCompressor Feb 09 '15 at 20:45
-
I think its fine. At least he didn't ask how to hook into a startup process like explorer.exe and have it run a .exe that starts and monitors another exe named jusched.exe. – Ya Wang Feb 09 '15 at 20:49
-
1this is a professional web-site and you should not talk to me like this as you have not got a proof and secondly by any law in the world you can not hold me on something that i did not say. – james Feb 09 '15 at 20:54
-
In order to delete the jar, you will need to know the name of the jar and its location relative to the current working directory. You can't delete the jar while it's been used, so you need to start a new JVM, which would allow the current JVM to close/exit (known as process jumping) and then delete the original jar...yeah for simplicity ... – MadProgrammer Feb 09 '15 at 21:00
4 Answers
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.

- 19,631
- 4
- 30
- 57
-
2And that's the trick. You can't delete the jar while it's been used by the JVM, but what you can do is start another JVM (with running the updater for example) and allow the original JVM to exit, freeing up the original jar... – MadProgrammer Feb 09 '15 at 21:01
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).
Windows strictly restricts you from doing this untill the jar is in use
It is something like this :
In Linux you can do it here is How to delete a executing jar file

- 1
- 1

- 7,643
- 6
- 34
- 62
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)

- 7,483
- 13
- 50
- 76

- 1,102
- 1
- 9
- 24