4

Given a reference to a File instance, is it possible to (programatically) detect whether the corresponding file is locked, and if so, which process is holding the lock?

I'm using Java 5, running on Windows.

Thanks, Don

Dónal
  • 185,044
  • 174
  • 569
  • 824
  • 1
    Check out http://stackoverflow.com/questions/1500174/checking-if-a-file-locked-in-java – svrist Aug 23 '10 at 08:27
  • 1
    Does this thread help? http://stackoverflow.com/questions/1390592/java-check-if-file-is-already-open – bdhar Aug 23 '10 at 08:28
  • 1
    I'm using Sysinternal's Procexp app for this detection which is famous for using low level and undocumented Windows apis, so I would never expect it to be possible in Java. But if there is a way, it would be great. – calavera.info Aug 23 '10 at 08:36
  • @calavera perhaps there is a way to wrap this in a java process. Does it have a Command Line interface? – Sean Patrick Floyd Aug 23 '10 at 08:48
  • @seanizer I don't think that there is a command line interface and besides the christian's solution is the right way to use native os features. – calavera.info Aug 24 '10 at 05:56
  • If you're willing to write c++ code, yes. But the question was about java. – Sean Patrick Floyd Aug 24 '10 at 06:26

1 Answers1

2

I think this is only possible in a native way. You need to access FileHandle informations. Write a Dll and connect it to your java app via JNI. Call NtQuerySystemInformation with SystemHandleInformation(16). You get a list with all open Handles, search for FileHandles, compare FileHandles with your own File.

christian
  • 391
  • 2
  • 10