Introduction to Jnotiy, a jar package that supports dynamic monitoring (cascading monitoring) folders and files. In Linux, the Jnotify service is called on the Linux underlying. In Windows, you need to add the DLL file for the attachment.
Because this jar file is not in the generic Maven repository, the Pom.xml file needs to be configured as follows:
<Dependency> <groupId>Net.contentobjects.jnotify</groupId> <Artifactid>Jnotify</Artifactid> <version>0.94</version> </Dependency> <!--without this package in the Central Library, you need to add the following repo - <repositories> <Repository> <ID>Bintray</ID> <URL>http://dl.bintray.com/typesafe/maven-releases/</URL> </Repository> </repositories>
View Code
Use
Import Java.lang.reflect.field;import java.util.arrays;import java.util.properties;import Org.apache.log4j.Logger; Import Net.contentobjects.jnotify.jnotify;import Net.contentobjects.jnotify.jnotifyexception;import Net.contentobjects.jnotify.jnotifylistener;public class Testjnotify {static Logger log = Logger.getlogger (TestJnotify . Class); /** * jnotify Dynamic Library-32-bit */static final String native_libraries_32bit = "/lib/native_libraries/32bits/"; /** * jnotify Dynamic Library-64-bit */static final String native_libraries_64bit = "/lib/native_libraries/64bits/"; public static void Main (string[] args) throws Jnotifyexception, Nosuchfieldexception, SecurityException, IllegalArgumentException, illegalaccessexception {log.debug ("-----------jnotify test---------"); Properties sysprops = System.getproperties (); String osarch = (string) sysprops.get ("Os.arch"); String osname = (string) sysprops.get ("Os.name"); String userdir = (string) SysproPs.getproperty ("User.dir"); Log.debug ("Os.arch:" + osarch); Log.debug ("Os.name:" + osname); Log.debug ("Userdir:" + userdir); Log.debug ("Java.class.path:" + sysprops.get ("Java.class.path")); An exception occurs when calling Jnotify directly: Java.lang.UnsatisfiedLinkError:no jnotify_64bit in Java.library.path//This is because Jnotify uses JNI technology to Loads the DLL file and throws this exception if no corresponding file is found under the classpath. You can therefore specify the path to the DLL file by specifying the program's startup parameters: Java-djava.library.path=/path/to/dll,//or by modifying the system variables at runtime of the JVM, as follows://Judgment System is 3 2bit or 64bit, the decision to call the corresponding DLL file String jnotifydir = native_libraries_64bit; if (!osarch.contains ("") ") {jnotifydir = Native_libraries_32bit; } log.debug ("Jnotifydir:" + jnotifydir); Get directory path String pathtoadd = Userdir + jnotifydir; Boolean isadded = false; Final Field Usrpathsfield = ClassLoader.class.getDeclaredField ("usr_paths"); Usrpathsfield.setaccessible (TRUE); Final string[] paths = (string[]) UsrPAthsfield.get (NULL); Log.debug ("usr_paths:" + arrays.tostring (paths)); for (String path:paths) {if (Path.equals (Pathtoadd)) {isadded = true; Break }} if (!isadded) {final string[] newpaths = arrays.copyof (paths, paths.length + 1); Newpaths[newpaths.length-1] = Pathtoadd; Usrpathsfield.set (null, newpaths); } log.debug ("Java.library.path:" + system.getproperty ("Java.library.path")); Log.debug ("usr_paths:" + arrays.tostring ((string[]) usrpathsfield.get (null))); Usrpathsfield.setaccessible (FALSE); Log.debug ("Classpath loading Complete"); Listen for file event Jnotify.addwatch ("f:\\", Jnotify.file_any, True, new Jnotifylistener () {@Override) under F disk public void filerenamed (int wd, String RootPath, String oldname, String newName) {log.debug ("WD =" + WD + ", RootPath =" + RootPath); Log.debug ("Oldname = "+ Oldname +", NewName = "+ NewName); } @Override public void filemodified (int wd, String RootPath, String fileName) {log. Debug ("Filemodified"); } @Override public void filedeleted (int wd, String RootPath, String fileName) {LOG.D Ebug ("filedeleted"); } @Override public void filecreated (int wd, String RootPath, String fileName) {LOG.D Ebug ("filedeleted"); } }); while (true) {}}}
How to use jnotify file monitoring and how to import jar files