The inotify-java API provides an event-based mechanism for monitoring Linux file system events using the inotify interface provided by glibc (versions 2.4 and up) and the Linux kernel, starting from 2.6.13.
Features
- Simple, easy to use
- Feature-complete support for inotify
- Low memory footprint
- API uses blocking calls yielding low CPU usage
Installation
- Extract the archive.
- tar xzf inotify-java-[version].tar.bz2
- Build it.
- ant release -Dversion=[version]
- Copy libinotify-java.so to a suitable location.
- cp build/native/libinotify-java.so /usr/lib
- Copy inotify-java-[version].jar to a suitable location.
- cp dist/inotify-java-[version].jar /usr/share/java/
Example
try {
Inotify i = new Inotify();
InotifyEventListener e = new InotifyEventListener() {
@Override
public void filesystemEventOccurred(InotifyEvent e) {
System.out.println("inotify event occurred!");
}
@Override
public void queueFull(EventQueueFull e) {
System.out.println("inotify event queue: " + e.getSource() +
" is full!");
}
};
i.addInotifyEventListener(e);
i.addWatch(System.getProperty("user.home"), Constants.IN_ACCESS);
} catch (UnsatisfiedLinkError e) {
System.err.println("unsatisfied link error");
} catch (UserLimitException e) {
System.err.println("user limit exception");
} catch (SystemLimitException e) {
System.err.println("system limit exception");
} catch (InsufficientKernelMemoryException e) {
System.err.println("insufficient kernel memory exception");
}Example Summary
- Create an instance.
- Add an event listener.
- Add watches.