Jnotify文件监控的用法以及Jar文件导入的方法
2016-05-23 23:00
405 查看
简介Jnotiy, 支持动态监控(支持级联监控)文件夹和文件的jar包。在linux中,调用linux底层的jnotify服务。在windows中,需要添加附件的dll文件。
因为通用的Maven仓库中没有此Jar文件,pom.xml文件需要如下配置:<dependency> <groupId>net.contentobjects.jnotify</groupId> <artifactId>jnotify</artifactId> <version>0.94</version> </dependency> <!-- 在central库中没有这个包,需要添加以下的repo --> <repositories> <repository> <id>bintray</id> <url>http://dl.bintray.com/typesafe/maven-releases/</url> </repository> </repositories>
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() { @Override 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.debug("fileDeleted"); } @Override public void fileCreated(int wd, String rootPath, String fileName) { log.debug("fileDeleted"); } }); while (true) { } } }
相关文章推荐
- Deepin 2015 体验版(Deepin Desktop Environment) 桌面环境 ubuntu下 安装
- Linux根文件系统构建---动态库
- PRO-1:Day1-linux基础
- ubantu下安装NVIDIA独立显卡驱动
- gdb调试
- 我的Android知识架构
- Nginx反向代理和负载均衡部署指南
- CentOS 6.6永久关闭 SELinux与防火墙 的方法
- shell(一)
- linux查看当前系统有几个账户
- Tomcat配置用户名和密码
- 第三方库Pop的简单使用
- bash:ll命令未找到问题
- OpenWrt 4G网卡拨号配置
- Linux用户环境
- apache 常用的两种工作模式和区别
- kali2.0源~~~摸着论坛学kali
- linux基本命令(31)——/etc/group文件详解
- IE如何禁止某个特定网站的Cookie
- HDU 5965 Gym Class 贪心+toposort