views:

324

answers:

4

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?

Thanks a lot. Grzesiek

+1  A: 

I think the correct thing to do here is to restart the application. Event if you could update the jar at runtime, all sort of errors might occur after because of class versions, different classes, different implementations, etc.

bruno conde
+3  A: 

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.

Matt
Thank You a lot, it gives me idea of a solution.; )
kogut
A: 

Typical way to do this is to write a separate updater which will be invoked by your main program when it sees an update. At this time your main program can start the updater in a new process and exit. You updater can wait for main program to exit, download and copy the updated files and restart your main program

Prashant
+2  A: 

That's a problem already solved by OSGi.

The real problem is not to load a new jar version. This could be done with your own classloader that releases the file lock. The real problem is how to handle instances of different versions of the same class.

Restart does omit these problem. If you really need different jar versions in one application use OSGi.

Thomas Jung