My testing environment is Windows 7 and Myeclipse is used.
1. Create a java or javaweb project...
2. decompress the jnotify-lib-0.94 package and add the jnotify-0.94.jar to the project (java project requires manual build to path, javaweb does not need ).
3. Set up the test class jpolicytest. java as follows and use junit for testing:
Package test; import net. contentobjects. jpolicy. JNotify; import net. contentobjects. jpolicy. jpolicyexception; import net. contentobjects. jpolicy. jpolicylistener; import org. junit. test; public class jpolicytest {/** monitored directory */String path = F: \ SHDownload \ PIC;/** monitored directory event */int mask = JNotify. FILE_CREATED | jpolicy. FILE_DELETED | jpolicy. FILE_MODIFIED | jpolicy. FILE_RENAMED;/** whether to monitor sub-directories, that is, cascading monitoring */boolean wat ChSubtree = true;/** listener 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 (jpolicytest. fileRenamed (): wd # + wd + root = + rootPath +, + oldName +-> + newName);} public void fileModified (int wd, S Tring rootPath, String name) {System. out. println (jpolicytest. fileModified (): wd # + wd + root = + rootPath +, + name);} public void fileDeleted (int wd, String rootPath, String name) {System. out. println (jpolicytest. fileDeleted (): wd # + wd + root = + rootPath +, + name);} public void fileCreated (int wd, String rootPath, String name) {System. out. println (jpolicytest. fileCreated (): wd # + wd + Root = + rootPath +, + name) ;}});} catch (jpolicyexception e) {e. printStackTrace ();} // an endless loop. The thread continues to run after one minute of sleep, the main purpose is to keep the main thread running // sleep time unrelated to the efficiency of the monitoring file (that is, it is not detected one minute after the directory file is changed, and the monitoring is almost real-time, call the local system library) while (true) {try {Thread. sleep (60000);} catch (InterruptedException e) {e. printStackTrace () ;}}@ Test public void removeWatch () {boolean res = false; try {res = JNotify. removeWatch (watchID);} catch (Jpolicyexception e) {e. printStackTrace ();} if (! Res) {// invalid watch ID specified .}}}
Right-click the beginWatch method and choose run as-junit test. The following error is returned:
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.
(Unknown Source) at net.contentobjects.jnotify.win32.JNotifyAdapterWin32.
(Unknown Source)
Error loading library, java.library.path=C:Program Files (x86)Javajdk1.6.0_30in;C:WindowsSunJavain;C:Windowssystem32;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:oracleproduct.2.0db_1in;C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShell1.0;C:Program Files (x86)Javajdk1.6.0_30in;D:ProBagapache-cxf-2.7.7in;.
This is because the class library for the response is not loaded when jnotify is running (the name of the class library cannot be changed in the compressed package downloaded above ):
The computer processor is i386 (intel32-bit processor) architecture: jnotify. dll
The computer processor uses the amd64 architecture: jpolicy_64bit.dll
In this case, you need to put the class library in System. err. println (System. getProperty (java. library. path); In any directory of the java class library path output by the code execution, I put it in the C: Program Files (x86) Javajdk1.6.0 _ 30in directory.
4. Run the command again. copy the file to the monitored directory. The output result is as follows: