2010-10-01 19 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中。然後嘗試它會開始工作。