开发者

how to check if a Java program is keeping open a handle to a file?

I have a Java program running under Windows XP. It reads a file and closes the file, or at least that's what it's supposed to do.

Sometimes the file remains locked and I can't write to it ("I" meaning me as a user, trying to execute copy file2.out file1.out from a Command Prompt, where file1.out is the file my Java progra开发者_如何学JAVAm reads) until I close my Java program. (windows complains "can't write to a file with a mapped user process" or something like that)

Any suggestions for how to debug? I'm stumped.


clarification: The problem I'm having isn't figuring out whether the file is being kept open, or by which process it's being kept open. The problem is figuring out where in my Java program I am not correctly closing the file, as it is a large program and several classes have access to the file's InputStream during the time the InputStream is opened.

I can narrow it down to a few classes, but I'm not sure where to look at next.


You may checkout Sysinternals Process Explorer which is an excellent tool allowing you to debug which process is holding a lock to the file handle. In the main menu click on Find -> Find Handle or DLL ... and then type the name of the file. It will show you the process which is holding the lock.

As far as your Java program is concerned make sure that you are always closing the files handle even if an exception is thrown (ideally in a finally statement).


FindBugs is great for finding those types of bugs and many others.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜