How to check if a Java program supports a file descriptor?

I have a Java program running under Windows XP. It reads the file and closes the file, or at least what it should do.

Sometimes a file remains locked and I cannot write it (ā€œIā€ means me as a user trying to execute copy file2.out file1.out from the command line, where file1.out is the file that my Java program reads) until while I close my Java program. (windows complain "cannot write to a file with a connected user process" or something like that)

Any suggestions for debugging? I'm at a dead end.


explanation . The problem I am facing is not to find out if the file is kept open, or by what process it remains open. The problem is that in my Java program, I do not close the file correctly, as it is a large program, and several classes have access to the InputStream file during the opening of the InputStream .

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

+4
source share
2 answers

You can check out the Sysinternals Process Explorer , which is a great tool for debugging which process holds a lock in a file descriptor. From the main menu, click Find -> Find Handle or DLL ... , and then enter the file name. It will show you the process that holds the lock.

For your Java program, make sure you always close the file descriptors, even if an exception is thrown (ideally in the finally statement).

+4
source

FindBugs is great for finding these types of errors and many more.

0
source

All Articles