javagarbage-collectionaccess-violationjnotify

Access violation when running jnotify sample code


I trying to run the modified code sample from http://jnotify.sourceforge.net/sample.html But getting the following error:

A fatal error has been detected by the Java Runtime Environment:

 EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6d980b1f, pid=7308, tid=10568

 JRE version: 6.0_45-b06
 Java VM: Java HotSpot(TM) Client VM (20.45-b01 mixed mode windows-x86 )
 Problematic frame:
 V  [jvm.dll+0xa0b1f]

I've seen this before, when there was no reference to the listener as in the unmodified code sample and the garbage collector removed it. But here I am holding a reference to it. Does anyone see the problem?

public static void sample() throws Exception {
    JNotifyListener listener = (JNotifyListener) new Listener();
    // path to watch
    String path = System.getProperty("D:/Pathname");

    // watch mask, specify events you care about,
    // or JNotify.FILE_ANY for all events.
    int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED
            | JNotify.FILE_MODIFIED | JNotify.FILE_RENAMED;

    // watch subtree?
    boolean watchSubtree = true;

    // add actual watch
    int watchID = JNotify.addWatch(path, mask, watchSubtree,
            listener);

    // sleep a little, the application will exit if you
    // don't (watching is asynchronous), depending on your
    // application, this may not be required
    Thread.sleep(1000000);

    // to remove watch the watch
    boolean res = JNotify.removeWatch(watchID);
    if (!res) {
        // invalid watch ID specified.
    }
}

static class Listener implements JNotifyListener {
    public void fileRenamed(int wd, String rootPath, String oldName,
            String newName) {
        print("renamed " + rootPath + " : " + oldName + " -> " + newName);
    }

    public void fileModified(int wd, String rootPath, String name) {
        print("modified " + rootPath + " : " + name);
    }

    public void fileDeleted(int wd, String rootPath, String name) {
        print("deleted " + rootPath + " : " + name);
    }

    public void fileCreated(int wd, String rootPath, String name) {
        print("created " + rootPath + " : " + name);
    }

    void print(String msg) {
        System.err.println(msg);
    }
}

Solution

  • You have a bug in your code. ;-)

    replace

    String path = System.getProperty("D:/Pathname");
    

    by

    String path = "D:/Pathname";