Reputation: 1777
My current project is writing a HTTP-Webserver in Java. The server checks a "deployment-directory" every two seconds and searches for a file named content.jar
. If the file was changed the server deployes it. The file contains servlets which will be loaded by an URLClassLoader
. On every deployment a new classloader is created.
In a different project my servlets are being developed. I wrote an Ant-buildfile that creates the content.jar
file and moves it into the server's deployment directory. Unfortunately I get an Ant error:
BUILD FAILED [path]\WebServerServlet\Ant\Build.xml:15: Unable to remove existing file [path]\WebServer\web\content.jar
I suppose that the file is locked on the filesystem by the JVM - is there a way to tell Ant it should not try to delete and move the file but to overwrite it instead?
Otherwise, does anyone have an idea how to avoid the file getting locked by the JVM by changing my deployment system?
EDIT
I have now found a solution! As discussed in this question the only thing i had to add was
URLClassLoader loader = new URLClassLoader(..);
//class loading stuff here
loader.close() //the line all the trouble was about
With this appendix my Ant-buildfile is able to delete the "old" jar in the deployment-directory and move the new one into it.
Upvotes: 0
Views: 80
Reputation: 34321
I suspect that the classloader has a lock on the jar. Either way until the lock is released you're not going to be able to move, delete or modify it.
The solution is to write your own classloader. It's this custom classloader that should be monitoring your jar for changes, not the server, then reloading the classes accordingly.
There's a lot involved in writing a classloader to do dynamic reloading - fortunately, it's explained here.
Upvotes: 1