2010-10-01 80 views
3

我正在尝试实现JNotify。但是当我编译程序时,我收到了一些奇怪的错误消息。我从这个网站获得示例代码ttp://jnotify.sourceforge.net/sample.htmlJNotify中的错误异常访问冲突

作为信息,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"); 
    } 
} 

但为什么是这样进行的错误的主类?我已经截图了错误,以便您可以通过点击看到它link

有没有人遇到过这种类型的错误?任何帮助将不胜感激。 谢谢

+0

对于代码示例,使用代码按钮(10101010)或缩进至少4个空格的所有内容。我为你修好了。 – 2010-10-01 17:15:16

+0

第一次使用这里。感谢您的纠正。 :-) – jacobian 2010-10-01 17:35:51

回答

2

JNotify确实使用JNI来与操作系统相关的通知API进行接口。看起来像是JNotify中存在一个错误。你有没有试过在SourceForge的JNotify论坛上提问?

+0

不,不过虽然只有几个关于JNotify的教程,我在google.c中找不到关于这个错误信息的任何信息,您告诉我另一种方法来做除JNotify之外的目录监视吗?我真的需要这种功能 – jacobian 2010-10-01 17:35:29

+0

有一个商业库JxFileWatcher(http://www.teamdev.com/jxfilewatcher)。如果你真的需要这个,并且不能让JNotify工作,你可能会考虑它(我与作者没有关系)。也做一个谷歌搜索“Java文件系统监视器” – 2010-10-01 18:27:44

0

我们遇到了同样的问题。因为我们无论如何都使用了JNA,所以我们只使用了这个框架中的FileMonitor示例。奇迹般有效。

0

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