Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete file which is used/locked by windows in java

I have some application, which has ability to update itself. Its downloads the jar file from the net, and then tries to replace used jar to run this application by downloaded one. But I cannot to do that, because the file is locked by windows, because application is still running.

Does anybody know some workaround of that?

like image 423
kogut Avatar asked Nov 17 '09 11:11

kogut


People also ask

How do I delete a locked file in Windows?

Open the Start menu, then the File Explorer and find the formerly locked file. Remove the file. Click the previously locked file, then the “Home” tab, and “Delete” in the toolbar. Or, you may select the file by clicking on it and then pressing the “Delete” key.

How do you delete a file that is used by another process in Java?

Delete a file with Java IO 2.1 For the legacy File IO java.io. * , we can use the File. delete() to delete a file, and it will return boolean, true if file deletion success, false otherwise.

How do you force delete a file in Java?

To force delete file using Java, we can use the FileUtils or FileDeleteStrategy class available in Apache Commons Io. We can also use FileDeleteStrategy class of apache commons io to force delete file, even if the file represents a non-enpty directory . the delete() method deletes the file object.


1 Answers

One of the popular solutions for this is to run your updater as a separate program. Have you ever noticed that Firefox has to restart when it is updated? Well that is because a separate process (updater.exe) is updating the files, then starting Firefox again.

You can try this approach. The only obstacle I see in the way is trying to automate the MAIN program to close itself. The only portable way to do this (in my head) is for the main application to wait for a kill signal via a local socket, and the updater can send the command through local networking. One more thing you have to consider is that the updater has to run in a separate java process. If your main program just creates a new Updater object, the Updater will co-exist with the main program's JVM, which brings you back to square one.

like image 144
Matt Avatar answered Sep 30 '22 13:09

Matt