Reputation: 83
I need to update my application. To do this, I delete the .jar file my program is currently running from and create a new one with the same name, then restart the application.
However, when I attempt to do this I get a java.io.IOException: Access is denied
.
How can I fix this?
Thanks!
Upvotes: 0
Views: 6774
Reputation: 347314
The problem is, the jar
files are been used by the JVM (specifically, the class loaders). Even under Java 7, where the jar files are closed by the class loaders when the are no longer needed, there is no guarantee that the underlying resources will be released, or more specifically, when they will be released.
For some more information, take a look at Closing a URLClassLoader
You have a few choices.
Separate your update process from your application (so it's a standalone program) and use parentless execution process to update the application. This involves executing your program in such away that it allows the current process to terminate before the new process, under windows this can be achieved using something like...
cmd /c start /b /normal "" {command line to be executed}
Under Linux I believe you can use nohup
, but I have no experience with that.
(Don't forget to use System.exit
to terminate the current process ;))
You could use Java Web Start which provides it's own updating capabilities
Upvotes: 3