I'm really not sure what to tell you on this one, because I took your code and it ran fine on my machine.
The only thing I removed was the throws InitializationError
, though I doubt that it has something to do with it.
Config: Mac OS X 10.9/Java 1.7.0_45/Intellij IDEA 12.1.6
P.S.
which itselves has subpackages from where I could delete/create the class
WatchService only watches the directory that you point to, if you want to watch the subdirectories you need to recursively walk and register each directory in the subtree.
EDIT:
public void processEvents() {
boolean finished = false;
while (!finished) {
// wait for key to be signaled
WatchKey key;
try {
key = watcher.take();
} catch (InterruptedException x) {
return;
}
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
switch (kind.name()) {
case "ENTRY_CREATE":
System.out.println("Creation has been detected in " + getDirName());
break;
case "ENTRY_DELETE":
System.out.println("Deletion has been detected in " + getDirName());
break;
case "ENTRY_MODIFY":
System.out.println("Modification has been detected in " + getDirName());
break;
default:
continue;
}
if (!key.reset()) {
finished = true;
}
}
}
}