0

I have written a Java application that has to monitor a live log file. However I have not thought of the possibly of the file being locked, and that is exactly the case now.

Is there a way to disable the ability to add filelocks in a directory under Windows? If I would succeed, what would happen to the program writing to the logs (I think it's written in C#).

I am suspecting the logger locks the file to prevent it from being deleted, I cannot see any reason why it would need to prevent reads on it.

Does anyone perhaps know another solution to "cancel" the read-part of a file lock from within Java, if that even makes sense?

skiwi
  • 66,971
  • 31
  • 131
  • 216
  • possible duplicate of [Java: opening and reading from a file without locking it](http://stackoverflow.com/questions/2537306/java-opening-and-reading-from-a-file-without-locking-it) – Roger Rowland Nov 19 '13 at 10:36
  • 1
    @RogerRowland as I understand it, this question is the other way around: the file is already locked, preventing it from being read – C.Evenhuis Nov 19 '13 at 11:05

1 Answers1

1

The entire idea of a lock is that when you acquire it, you have some sort of guarantee that you have exclusive access (of the desired type(s)) to the file. Perhaps the programmer of the other application made a mistake or did not give much thought to how he/she locked the file.

I don't think there are clean ways to bypass this problem (except hooking the OS calls?). You could open the file before running the other application, but chances are the application wouldn't even start up in that case.

C.Evenhuis
  • 25,996
  • 2
  • 58
  • 72
  • Just a quick update: It might that the file actually allow reading (someone was able to open it -and monitor it live- with Notepad++ on the server). I however had a dirty hack in my program that used a `FileWriter` to check if the file was readable, had something to do with that the files were being copied over from another directory I think and the `JNotify` plugin would catch that file at the moment it had been created. – skiwi Nov 19 '13 at 12:28