: Http://sourceforge.net/projects/jnotify/
The usage instructions of jpolicyare as follows:
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/2 k/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.
The following is the main function I extracted from net. contentobjects. jnotify. jnotify, which can be compiled and run directly.
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);}}
I operate on the command line used in Linux and run the demo above. Before running, link the jnotify-0.94.jar to the dynamic library corresponding to the Operating System (I'm using 64-bit libjnotify. So ). The following problems may occur during running:
1. the class in the jnotify-0.94.jar cannot be found during compilation, you can set the classpath path, you can also use the following command line parameters for compilation:
Javac-CP.: jnotify-0.94.jar jpolicytest. Java
-Do not add the current directory after CP.
2. Run the following command:
Java-CP.: jnotify-0.94.jar jpolicytest
The following error is reported:
Exception in thread "Main" Java. Lang. unsatisfiedlinkerror: No jnotify in Java. Library. Path
This is because libjnotify cannot be found in the search path of the dynamic link library when the program is running. so (the preceding Lib is the prefix, followed. so is the suffix). In Bash, you can complete the following settings,
Export LD_LIBRARY_PATH = $ LD_LIBRARY_PATH :.
3. After the search path of the dynamic link library is set successfully, the following problems may occur:
Exception in thread "Main" Java. Lang. unsatisfiedlinkerror:/usr/local/study/jpolicy3/libjnotify. So:/lib64/libc. so.6: Version 'glibc _ 100' not found
This problem is that the glibc library version of the system is not libjnotify. the version used for so compilation. Use rpm-Qi glibc to view the glibc library version information of the system. Here, my version number is 2.5, which is lower than libjnotify. so version 2.12 used for compilation.
My solution is to re-compile libjnotify. So on the local machine, as follows:
· First, copy the following three files in the source code to a directory.
Net_contentobjects_jnotify_linux_jnotify_linux.c inotify-syscalls.h net_contentobjects_jnotify_linux_jnotify_linux.h
· Then compile as follows:
-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_jpolicy_linux_jpolicy_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_jpolicy_linux_jpolicy_linux.h
Libjnotify. O net_contentobjects_jpolicy_linux_jpolicy_linux.c
Here we need to introduce the library required by JNI, and the version must match the operating system. Use uname-a to view the operating system version and Java-version to view the JDK version. Here, the operating system and JDK are both 64-bit.
Libjnotify. So can be used normally.
The correct execution process is as follows:
-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
The following are my reference articles. Thank you for sharing them.
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