5

I know this was asked elsewhere a year ago but I'm looking for any updates please.

I have a program in Java 7 that uses WatchService to monitor a directory for new files being created there. If I register a directory on my local machine (e.g. c:\NewFiles) then all is fine. However when I try to get it to listen to a folder on a network drive it compiles and runs but exits straight away because it doesn't seem to get notified of any events.. here is just a fragment of my code...

 private final WatchService watcher;
 private final Map<WatchKey, Path> keys;
        this.watcher = FileSystems.getDefault().newWatchService();
        this.keys = new HashMap<>();

I register the directory...

String srcDirPath = "G:\\NewFiles";
Path dir = Paths.get(srcDirPath);
    WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
    keys.put(key, dir);

then at the end of the loop that checks for events...

        if (keys.isEmpty()) {
                System.out.println("Drive is inaccessible");
                break;
            }

Here I always get the "Drive is inaccessible" warning. I tried copying my Java files to this network directory and running them from there but I get the same problem.

Is it possible to run Watchservice like this from a local machine to listen to changes to a directory on a network drive? Am I getting a permission or firewall problem here? I am using Windows 7 and most likely the network drive is also on a Windows 7 machine.

FlexMcMurphy
  • 420
  • 5
  • 21
  • `FileSystems.getDefault()` is same for all Windows drives on my machine, but it would be safer to use `dir.getFileSystem()` before `newWatchService()` in case they aren't the same implementations. Also, how are the events being read? – DuncG Jun 13 '22 at 18:49

0 Answers0