企业网络管理 网站开发与运营,室内设计公司排行,柬埔寨网赌网站开发,广东省广州市佛山市简介Jnotiy, 支持动态监控(支持级联监控)文件夹和文件的jar包。在linux中#xff0c;调用linux底层的jnotify服务。在windows中#xff0c;需要添加附件的dll文件。因为通用的Maven仓库中没有此Jar文件#xff0c;pom.xml文件需要如下配置#xff1a;net.contentobjects.jn…简介Jnotiy, 支持动态监控(支持级联监控)文件夹和文件的jar包。在linux中调用linux底层的jnotify服务。在windows中需要添加附件的dll文件。因为通用的Maven仓库中没有此Jar文件pom.xml文件需要如下配置net.contentobjects.jnotifyjnotify0.94bintrayhttp://dl.bintray.com/typesafe/maven-releases/View Code使用首先从jar包中解目录压出dll文件,并放到工程lib/目录下。比如/project/lib/native_libraries/...测试代码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动态库 - 32位*/static final String NATIVE_LIBRARIES_32BIT /lib/native_libraries/32bits/;/*** jnotify动态库 - 64位*/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));// 直接调用Jnotify时 会发生异常java.lang.UnsatisfiedLinkError: no jnotify_64bit in java.library.path// 这是由于Jnotify使用JNI技术来加载dll文件如果在类路径下没有发现相应的文件就会抛出此异常。// 因此可以通过指定程序的启动参数: java -Djava.library.path/path/to/dll// 或者是通过修改JVM运行时的系统变量的方式来指定dll文件的路径如下// 判断系统是32bit还是64bit决定调用对应的dll文件String jnotifyDir NATIVE_LIBRARIES_64BIT;if (!osArch.contains(64)) {jnotifyDir NATIVE_LIBRARIES_32BIT;}LOG.debug(jnotifyDir: jnotifyDir);// 获取目录路径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(类路径加载完成);// 监听F盘下的文件事件JNotify.addWatch(F:\\, JNotify.FILE_ANY, true, new JNotifyListener() {Overridepublic void fileRenamed(int wd, String rootPath, String oldName, String newName) {log.debug(wd wd , rootPath rootPath);log.debug(oldName oldName , newName newName);}Overridepublic void fileModified(int wd, String rootPath, String fileName) {log.debug(fileModified);}Overridepublic void fileDeleted(int wd, String rootPath, String fileName) {log.debug(fileDeleted);}Overridepublic void fileCreated(int wd, String rootPath, String fileName) {log.debug(fileDeleted);}});while (true) {}}}