This discussion is archived
0 Replies Latest reply: Dec 15, 2010 9:20 AM by 824231 RSS

Java 7 WatchService: Key Always Invalid when Using Mapped Drive

824231 Newbie
Currently Being Moderated
Hi

I'm am attempting to use the WatchService API in Java 7 to poll for changes in a directory, which I've mapped on my Windows Desktop.

So, in other words, I have a drive mapped to "h:", and so upon registering the the Path, and then creating the WatchKey with the watcher, the key is always invalid! However, when I do the same for a directory on my local drive (ie. "c:\test"), it works fine.

Here's the code

public static void main(String[] args)
{
WatchService watcher = null;
Path dir = null;

try
{
watcher = FileSystems.getDefault().newWatchService();
dir = Paths.get("h:\\Inbox\\BOA");

dir.register(watcher, StandardWatchEventKind.ENTRY_CREATE, StandardWatchEventKind.ENTRY_DELETE);



}
catch (IOException x) {
System.err.println(x);
}
catch(Exception e)
{
e.printStackTrace();
}

for (;;) {

System.out.println("POLLING");

//wait for key to be signaled
WatchKey key;
try
{
key = watcher.take();
if(key.isValid())
System.out.println("Key Invalid!");
}
catch (Exception x) {

     return;
}

for (WatchEvent<?> event: key.pollEvents()) {
     WatchEvent.Kind<?> kind = event.kind();

//This key is registered only for ENTRY_CREATE events,
//but an OVERFLOW event can occur regardless if events are
//lost or discarded.
     if (kind == StandardWatchEventKind.OVERFLOW) {
     continue;
     }
if( kind == StandardWatchEventKind.ENTRY_CREATE)
{
WatchEvent<Path> ev = (WatchEvent<Path>)event;
     Path filename = ev.context();
Path child = dir.resolve(filename);
System.out.println();
}
else if(kind == StandardWatchEventKind.ENTRY_DELETE)
{
System.out.println();
}


}// for

//Reset the key -- this step is critical if you want to receive
//further watch events. If the key is no longer valid, the directory
//is inaccessible so exit the loop.
boolean valid = key.reset();
if (!valid) {
break;
}
}

}
}


Any ideas why the key is al

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points