3

I am trying to monitor a remote folder using WatchService (java.nio.file.*). Everything works fine for local folders. However I was unable to figure out how to monitor a remote share. Can I pass credentials along?

(If the user executing the code has the rights to mount the share it works as well.)

Here are parts of my code:

  public void lunch() throws IOException {
        boolean recursive = true;
        Path dir = Paths.get("C:\\test");
        new Watch(dir, recursive).processEvents();
    }

    public Watch(Path dir, boolean recursive) throws IOException {
        this.watcher = FileSystems.getDefault().newWatchService();
        this.keys = new HashMap<WatchKey,Path>();
        this.recursive = recursive;

        if (recursive) {
            System.out.format("Scanning %s ...\n", dir);
            registerAll(dir);
            System.out.println("Done.");
        } else {
            register(dir);
        }
}

Cheers, Stephanie

Stephanie
  • 101
  • 1
  • 6
  • can you please explain your statement - "If the user executing the code has the rights to mount the share it works as well". Were you able to monitor remote directory using WatchService? – CleanBold Jun 25 '21 at 10:25

1 Answers1

4

From WatchService javadoc

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.

pba
  • 700
  • 8
  • 18
  • 1
    do you have any examples on how to implement the monitoring in the remote system? – Ianthe Feb 22 '18 at 02:57
  • I have a less complex problem. I am running a watcher service on linux machine, monitoring the folders on that same server. But the watcher is only picking up files when I manually push the documents in those folders (through WinSCP/SSH). The actual scenario is when the files come from Scanner and Fax machines, and sit in these folders. In this case, the watcher service is not picking up files. Any idea what can be the issue ? I already checked the permissions on the incoming file. It is with the root user only, the same user with which my watcher service is running. – Sanket Mehta Oct 07 '19 at 11:27
  • 2
    I resolved the isue with the help of this link - https://stackoverflow.com/questions/48919086/java-watch-service-not-working-for-remote-files-mounted-in-the-local-server?rq=1 – Sanket Mehta Oct 17 '19 at 05:46