{"record":{"id":"cf010241137042a3","repo":"java-native-access/jna","slug":"no-ancestor-found-for-file","errorCode":null,"errorMessage":"No ancestor found for <file>","messagePattern":"No ancestor found for <file>","errorType":"exception","errorClass":"FileNotFoundException","httpStatus":null,"severity":"error","filePath":"contrib/platform/src/com/sun/jna/platform/win32/W32FileMonitor.java","lineNumber":181,"sourceCode":"        }\n        return result;\n    }\n\n    private static int watcherThreadID;\n\n    @Override\n    protected synchronized void watch(File file, int eventMask, boolean recursive) throws IOException {\n        File dir = file;\n        if (!dir.isDirectory()) {\n            recursive = false;\n            dir = file.getParentFile();\n        }\n        while (dir != null && !dir.exists()) {\n            recursive = true;\n            dir = dir.getParentFile();\n        }\n        if (dir == null) {\n            throw new FileNotFoundException(\"No ancestor found for \" + file);\n        }\n        Kernel32 klib = Kernel32.INSTANCE;\n        int mask = WinNT.FILE_SHARE_READ\n            | WinNT.FILE_SHARE_WRITE | WinNT.FILE_SHARE_DELETE;\n        int flags = WinNT.FILE_FLAG_BACKUP_SEMANTICS\n            | WinNT.FILE_FLAG_OVERLAPPED;\n        HANDLE handle = klib.CreateFile(file.getAbsolutePath(),\n                WinNT.FILE_LIST_DIRECTORY,\n                mask, null, WinNT.OPEN_EXISTING,\n                flags, null);\n        if (WinBase.INVALID_HANDLE_VALUE.equals(handle)) {\n            throw new IOException(\"Unable to open \" + file + \" (\"\n                                  + klib.GetLastError() + \")\");\n        }\n        int notifyMask = convertMask(eventMask);\n        FileInfo finfo = new FileInfo(file, handle, notifyMask, recursive);\n        fileMap.put(file, finfo);\n        handleMap.put(handle, finfo);","sourceCodeStart":163,"sourceCodeEnd":199,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/win32/W32FileMonitor.java#L163-L199","documentation":"W32FileMonitor.watch walks up the parent chain of the requested file to find the nearest existing ancestor directory to watch (marking recursive=true). If it climbs all the way to the root and no ancestor exists, it throws FileNotFoundException because there is no valid directory to pass to CreateFile.","triggerScenarios":"Calling W32FileMonitor.addWatch with a path whose every ancestor is non-existent (e.g. a deeply nested path on a drive/mount point that does not exist, or a malformed path like 'Z:\\nonexistent-drive\\a\\b').","commonSituations":"Typos in drive letters or UNC paths; watching a path on a disconnected network share; watching a path under a mount point that is not mounted yet.","solutions":["Ensure at least the parent directory (or the drive root) of the target path exists before calling addWatch","Create the missing ancestor directories yourself, then add the watch","Validate the path (drive letter, UNC host/share availability) before registering the watch","Catch FileNotFoundException and surface a clear message to the user about the invalid watch path"],"exampleFix":"// before\nmonitor.addWatch(new File(\"X:\\\\missing\\\\sub\"), listener);\n// after\nFile dir = new File(\"X:\\\\missing\\\\sub\");\nif (dir.getParentFile() == null || !dir.getParentFile().exists()) {\n    throw new IllegalArgumentException(\"Watch path has no existing ancestor: \" + dir);\n}\nmonitor.addWatch(dir, listener);","handlingStrategy":"validation","validationCode":"File dir = file.getAbsoluteFile();\nwhile (dir != null && !dir.exists()) { dir = dir.getParentFile(); }\nif (dir == null) throw new IllegalArgumentException(\"No existing ancestor for \" + file);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate the drive/root exists before addWatch","Create the target directory tree before registering the watch","Verify UNC paths / drive letters are reachable"],"tags":["win32","file-monitoring","file-not-found"],"backgroundTag":"file-not-found","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}