JNotify 的簡單使用(Linux命令列環境)

來源:互聯網
上載者:User

:http://sourceforge.net/projects/jnotify/
關於JNotify的使用說明,從下載的jnotify-lib-0.94.zip中解壓出的readme檔案的前兩行寫的很清晰,如下:
JNotify works on Linux with INotify support (Tested on 2.6.14), Mac OS X 10.5 or higher (Tested on 10.6.2), and on Windows XP/2K/NT (Tested on XP and on Windows 7).
You can test run JNotify with the command:
java -Djava.library.path=. -jar jnotify-0.94.jar [directory]
which will monitor the specified directory or the current directory if not specified and output events to the console.

下面是我從net.contentobjects.jnotify.JNotify中取出的main函數,可以直接編譯運行

import java.io.File;import java.io.IOException;import net.contentobjects.jnotify.JNotify;import net.contentobjects.jnotify.JNotifyListener;public class JNotifyTest {public static void main(String[] args) throws InterruptedException, IOException{String dir = new File(args.length == 0 ? "." : args[0]).getCanonicalFile().getAbsolutePath();JNotify.addWatch(dir, JNotify.FILE_ANY, true, new JNotifyListener(){public void fileRenamed(int wd, String rootPath, String oldName,String newName){System.out.println("renamed " + rootPath + " : " + oldName + " -> " + newName);}public void fileModified(int wd, String rootPath, String name){System.out.println("modified " + rootPath + " : " + name);}public void fileDeleted(int wd, String rootPath, String name){System.out.println("deleted " + rootPath + " : " + name);}public void fileCreated(int wd, String rootPath, String name){System.out.println("created " + rootPath + " : " + name);}});System.out.println("Monitoring " + dir/* + ", ctrl+c to stop"*/);while (true) Thread.sleep(10000);}}

我是在Linux下使用的命令列進行操作的,運行上面的demo。運行前,將jnotify-0.94.jar和作業系統對應的動態連結程式庫(我用的是64位的libjnotify.so)。運行時,會出現以下問題:
1.編譯時間找不到jnotify-0.94.jar中的類,可以設定classpath路徑,也可以使用下面的命令列參數,進行編譯:
  javac -cp .:jnotify-0.94.jar JNotifyTest.java
  -cp 後面不要忘加目前的目錄.
2.運行時,執行下面的命令:
  java -cp .:jnotify-0.94.jar JNotifyTest
  這時會報下面的錯誤:
  Exception in thread "main" java.lang.UnsatisfiedLinkError: no jnotify in java.library.path
  這是因為程式運行時在動態連結程式庫的搜尋路徑中找不到libjnotify.so(前面的lib是首碼,後面的.so是尾碼),在bash下可以通過以下設定完成,
  export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.
3.動態連結程式庫搜尋路徑設定成功以後,可能還會出現下面的問題:
  Exception in thread "main" java.lang.UnsatisfiedLinkError: /usr/local/study/jnotify3/libjnotify.so: /lib64/libc.so.6: version `GLIBC_2.12' not found
  這個問題在於系統的glibc庫版本不是libjnotify.so編譯時間使用的版本,使用 rpm -qi glibc 查看系統的glibc庫版本資訊,在這裡看到我的版本號碼是2.5,低於libjnotify.so編譯時間使用的版本2.12。
  我的處理方法是在本機重新編譯libjnotify.so,如下:
  ·首先將原始碼中的以下三個檔案拷貝到某一目錄下
  net_contentobjects_jnotify_linux_JNotify_linux.c inotify-syscalls.h net_contentobjects_jnotify_linux_JNotify_linux.h
  ·然後進行編譯,如下
  -bash-3.1$ gcc -I /usr/java/jdk1.6.0_45/include/ -I /usr/java/jdk1.6.0_45/include/linux/ -fPIC -g -c net_contentobjects_jnotify_linux_JNotify_linux.c -o libjnotify.o
  -bash-3.1$ gcc -g -shared -W1 -o libjnotify.so libjnotify.o -lc
  -bash-3.1$ ls
  inotify-syscalls.h  libjnotify.so                                     net_contentobjects_jnotify_linux_JNotify_linux.h
  libjnotify.o        net_contentobjects_jnotify_linux_JNotify_linux.c
  這裡要引入 jni 需要的庫,而且版本要和作業系統相匹配。使用 uname -a 查看作業系統的版本,java -version 查看JDK版本。我這裡作業系統和JDK都是64位版本。
  此時可以正常使用libjnotify.so了。

正確的執行過程如下:

-bash-3.1$ lsjnotify-0.94.jar  JNotifyTest.java  libjnotify.so-bash-3.1$ javac -cp .:jnotify-0.94.jar JNotifyTest.java -bash-3.1$ java -cp .:jnotify-0.94.jar JNotifyTest-bash-3.1$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.-bash-3.1$ java -cp .:jnotify-0.94.jar JNotifyTestMonitoring /usr/local/study/jnotify3

下面是我參考的文章,感謝他們的分享

http://blog.csdn.net/qq160816/article/details/7679426

http://blog.csdn.net/force_eagle/article/details/8684669

http://blog.csdn.net/hmsiwtv/article/details/8197263

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.