The Java.nio.file package did not detect directory changes

The Java.nio.file package did not detect directory changes … here is a solution to the problem.

The Java.nio.file package did not detect directory changes

I

want to detect the file that is writing to the directory, so I think a Java NIO package is more appropriate. However, I’ve run their test code ( https://docs.oracle.com/javase/tutorial/essential/io/examples/WatchDir.java )
It will only detect file changes made by the machine running the script.
That is, we have multiple servers running and they share many installed drives. If I log in to one of these servers, run the test code, log in to the same machine again through another terminal and change the directory I’m watching, those changes will be detected. However, if I log in to a different server, these changes do not occur. Is this a fundamental question for NIO? Should I use something else in this case? Or is there a workaround?

Solution

It’s kind of trying 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 poll manually on a regular basis.

Related Problems and Solutions