views:

53

answers:

1

Hi,

I'm writing a Maven plugin that deletes and renames various files using the File.delete() and File.renameTo(File) JDK methods.

Roughly every second time I run the plugin, one of these operations fails, and each time it fails it's a different file that cannot be deleted or renamed. An obvious explanation for why a file cannot be deleted is that another process is using it (I'm running on Windows), but I've no idea which process might be responsible. The fact that the problem cannot be reproduced consistently suggests a threading issue, but AFAIK, Maven plugins are run in a single thread. It's difficult to get any information about the cause of the problem, because the methods referred to above don't throw exceptions, they just return false.

Is there a way to programatically detect a locked file and the name of the process holding the lock? Alternatively, if anyone has other suggestion about how to go about debugging a problem such as this one, please send them on.

Thanks, Don

+1  A: 

Handle can let you find out what processes have handles on files.

Sample output (it's a command line utility):

C:\Users\Jon\Downloads\Handle>handle Test.cs

Handle v3.42
Copyright (C) 1997-2008 Mark Russinovich
Sysinternals - www.sysinternals.com

Test.exe           pid: 6088    190: C:\Users\Jon\Test\Test.cs
Jon Skeet
I don't think this will help me much, because I don't know before I start the program which file will fail to be deleted (or renamed).
Don
@Don: So you run it afterwards... chances are the same process will still be holding onto the resource.
Jon Skeet