jnotify-0.94 下载地址:http://download.csdn.net/detail/zxnlmj/7499157
本人测试环境为win7环境,使用Myeclipse
1、建一个java或者javaweb项目。。。
2、将jnotify-lib-0.94压缩包解压缩,添加其中的jnotify-0.94.jar到项目中(java项目需要手动build to path,javaweb不需要)。
3、建立测试类JnotifyTest.java如下,使用junit进行测试:
package test; import net.contentobjects.jnotify.JNotify; import net.contentobjects.jnotify.JNotifyException; import net.contentobjects.jnotify.JNotifyListener; import org.junit.Test; public class JnotifyTest { /** 被监视的目录 */ String path = "F:\\SHDownload\\PIC"; /** 监视目录的事件 */ int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED | JNotify.FILE_MODIFIED| JNotify.FILE_RENAMED; /** 是否监视子目录,即级联监视 */ boolean watchSubtree = true; /** 监听程序Id */ public int watchID; @Test public void beginWatch() { try { this.watchID = JNotify.addWatch(path, mask, watchSubtree, new JNotifyListener() { public void fileRenamed(int wd, String rootPath, String oldName, String newName) { System.out.println("JNotifyTest.fileRenamed() : wd #" + wd + " root = " + rootPath + ", " + oldName + " -> " + newName); } public void fileModified(int wd, String rootPath, String name) { System.out.println("JNotifyTest.fileModified() : wd #" + wd + " root = " + rootPath + ", " + name); } public void fileDeleted(int wd, String rootPath, String name) { System.out.println("JNotifyTest.fileDeleted() : wd #" + wd + " root = " + rootPath + ", " + name); } public void fileCreated(int wd, String rootPath, String name) { System.out.println("JNotifyTest.fileCreated() : wd #" + wd + " root = " + rootPath + ", " + name); } }); } catch (JNotifyException e) { e.printStackTrace(); } // 死循环,线程一直执行,休眠一分钟后继续执行,主要是为了让主线程一直执行 // 休眠时间和监测文件发生的效率无关(就是说不是监视目录文件改变一分钟后才监测到,监测几乎是实时的,调用本地系统库) while(true){ try { Thread.sleep(60000); } catch (InterruptedException e) { e.printStackTrace(); } } } @Test public void removeWatch(){ boolean res = false; try { res = JNotify.removeWatch(watchID); } catch (JNotifyException e) { e.printStackTrace(); } if (!res) { // invalid watch ID specified. } } }
选中beginWatch方法,右键,run as -》junit test,报错如下:
java.lang.UnsatisfiedLinkError: no jnotify in java.library.path at java.lang.ClassLoader.loadLibrary(Unknown Source) at java.lang.Runtime.loadLibrary0(Unknown Source) at java.lang.System.loadLibrary(Unknown Source) at net.contentobjects.jnotify.win32.JNotify_win32.<clinit>(Unknown Source) at net.contentobjects.jnotify.win32.JNotifyAdapterWin32.<init>(Unknown Source)
Error loading library, java.library.path=C:\Program Files (x86)\Java\jdk1.6.0_30\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32; C:\Windows;C:/Program Files (x86)/MyEclipse8.6/Common/binary/com.sun.java.jdk.win32.x86_1.6.0.013/jre/bin/client; C:/Program Files (x86)/MyEclipse8.6/Common/binary/com.sun.java.jdk.win32.x86_1.6.0.013/jre/bin;C:\oracle\product\10.2.0\db_1\bin; C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\; C:\Program Files (x86)\Java\jdk1.6.0_30\bin;D:\ProBag\apache-cxf-2.7.7\bin;.
这是因为没有将jnotify运行时需要加载响应的类库(上面下载的压缩包中,类库的名称不能随意改变):
电脑处理器为i386(intel32位处理器)架构:jnotify.dll
电脑处理器为amd64架构:jnotify_64bit.dll
这就需要将类库放到System.err.println(System.getProperty("java.library.path"));该代码执行所输出的java类库路径的任意一个目录下,我是放到C:\Program Files (x86)\Java\jdk1.6.0_30\bin该目录下。
4、再次运行,成功;往监视目录中拷贝文件,输出结果如下:
java中Jnotify文件监视库的使用
时间: 2024-10-26 11:13:04