Java WatchService not generating events while watching mapped drives

I have the same issue trying to watch a mounted windows share via CIFS. It seems not possible to get filesystem events for CIFS mounts. The linux implementation of the Java 7 NIO FileWatcher uses inotify. Inotify is a linux kernel subsystem to notice filesystem changes which works perfect for local directories, but apparently not … Read more

Java 7 WatchService – Ignoring multiple occurrences of the same event

WatcherServices reports events twice because the underlying file is updated twice. Once for the content and once for the file modified time. These events happen within a short time span. To solve this, sleep between the poll() or take() calls and the key.pollEvents() call. For example: @Override @SuppressWarnings( “SleepWhileInLoop” ) public void run() { setListening( … Read more

Can I watch for single file change with WatchService (not the whole directory)?

Just filter the events for the file you want in the directory: final Path path = FileSystems.getDefault().getPath(System.getProperty(“user.home”), “Desktop”); System.out.println(path); try (final WatchService watchService = FileSystems.getDefault().newWatchService()) { final WatchKey watchKey = path.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY); while (true) { final WatchKey wk = watchService.take(); for (WatchEvent<?> event : wk.pollEvents()) { //we only register “ENTRY_MODIFY” so the context is always … Read more