JNotify中的错误异常访问冲突

3

我正在尝试实现JNotify,但在编译程序时遇到了一些奇怪的错误消息。我从这个网站ttp://jnotify.sourceforge.net/sample.html获取了示例代码。

作为信息,JNotify用于目录监视,这是我的源代码的样子。

这是类watching.java的内容。

import net.contentobjects.jnotify.JNotifyListener;
import net.contentobjects.jnotify.JNotify;


public class watching{

public void watching(String s) throws Exception {
    // path to watch
    String path = System.getProperty(s);

    // 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, new 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.
    }
  }
  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);
    }
  }
}

这是名为nowwatch.java的主类。

public class nowwatch
{
    public static void main(String[] args) throws Exception 
    {
        System.out.println("Hello World!");
        watching hello = new watching();
        hello.watching("C:/Users/Raden/Documents/Downloads");
    }
}

但是为什么会出现这样的错误呢?我已经截图了这个错误,你可以点击链接查看。

有人遇到过这种错误吗?任何帮助都将不胜感激。谢谢。


对于代码示例,请使用代码按钮(10101010)或至少缩进4个空格。我已经为您修复了它。 - Jim Garrison
第一次使用者在这里。感谢您的纠正。 :-) - jacobian
3个回答

2

JNotify 一定使用 JNI 来与依赖于操作系统的通知 API 进行接口交互。看起来 JNotify 存在一个 bug。你尝试在 SourceForge 上的 JNotify 论坛上询问了吗?


不,目前还没有。关于JNotify只有一些教程,我在谷歌上也找不到有关这个错误信息的任何信息。你能告诉我其他监视目录的方法吗?我真的需要这种功能。 - jacobian
有一个商业库JxFileWatcher(http://www.teamdev.com/jxfilewatcher)。如果你真的需要它,而且无法让JNotify工作,那么你可以考虑使用它(我与作者没有任何联系)。同时,在谷歌上搜索“java文件系统监视器”。 - Jim Garrison

0

它要求 jNotify.dll 文件,请确保您已将该文件放置在窗口或 jre/bin 或 jdk/bin 中,然后尝试它将开始工作。


0
我们也遇到了同样的问题。因为我们毕竟使用 JNA,所以我们只需使用该框架中的FileMonitor示例即可。这个方法非常有效。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接