Search code examples
javanio

java.nio.file package not detecting directory changes


I want to detect files being written to a directory and so thought the Java NIO package would be suitable. However, I've ran their tester code (https://docs.oracle.com/javase/tutorial/essential/io/examples/WatchDir.java) and it will only detect file changes made by the machine running the script. i.e. we have multiple servers running which share a number of mounted drives. If I log into one of these servers, run the test code, log into the same machine again via another terminal and make changes to the directory I'm watching, these changes will be detected. However, if I log into a different server, these changes are not. Is this a fundamental problem with NIO, in which case is there something else I should use, or is there a workaround?


Solution

  • It kinda tries to warn you: WatchService

    Platform dependencies

    [...]

    If a watched file is not located on a local storage device then it is implementation specific if changes to the file can be detected. In particular, it is not required that changes to files carried out on remote systems be detected.

    I'm afraid you'll need to periodically poll manually.