Re: WELCOME to [email protected]

Stefan Bodewig <[email protected]>
Newsgroups gmane.comp.jakarta.ant.user
Message-ID <[email protected]>
On 2015-06-14, Christopher BROWN wrote:

> Since Java SE 7, URLClassLoader defines a "close()" method which may be
> useful in the specific case mentioned:
> http://docs.oracle.com/javase/7/docs/api/java/net/URLClassLoader.html#close()

This won't help if the file in question is on the system classloader or
the classloader is going to be used later.

> As for the more general case of Windows locking files from Java, as I
> understand it, it's because the underlying file descriptor use to read from
> or write to files is only release when the associated stream is garbage
> collected (the logic is in the "finalize()" method).  In my own code, I
> have a utility "delete(File)" method that tries to delete the file, and if
> it fails (on a Windows system) will loop with 10 millisecond sleep
> intervals, on the second and last iterations attempting System.gc() to
> force finalization, and it is seems to be a robust workaround in practice.

Ant has similar code.  In 1.7.0 it was restricted to specific cases like
the Delete task, nowadays it is a helper method (tryHardToDelete in
FileUtils) that has been factored out and is used whenever a task wants
to delete a file.

Unfortunately it doesn't help for all cases (like files on the
classloader) and that's when the deleteOnExit work around we've added
comes into play.

Stefan
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.