I need to monitor a given directory and all its sub directories for changes (Especially addition of files and directories).
My current code is as follows
Path watchFolder = Paths.get("D:/watch");
    WatchService watchService = FileSystems.getDefault().newWatchService();
    watchFolder.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);
            watchFolder.register(watchService, StandardWatchEventKinds.ENTRY_DELETE);
            watchFolder.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
    boolean valid = true;
    do {
        WatchKey watchKey = watchService.take();
        for (WatchEvent event : watchKey.pollEvents()) {
            WatchEvent.Kind kind = event.kind();
            if (StandardWatchEventKinds.ENTRY_CREATE.equals(kind)) {
                String fileName = event.context().toString();
                System.out.println("File Created:" + fileName);
            }
        }
        valid = watchKey.reset();
    } while (valid);
This code is only able to monitor the parent directory changes. If I add a directory in the parent directory, it is able to fire the event. But, if I add a file inside sub-directory, it is not able detect.
FYI : I also tried JNotify, but it keeps saying java.lang.UnsatisfiedLinkError: no jnotify_64bit in java.library.path
Is there any better solution than these?