Skip to content

Monitoring filesystem changes using Java

Updated: at 01:24 AM

During the past few months, I have chances to work in projects which have the requirement to monitor a specific folder on the filesystem for changes and act accordingly (e.g import structured file from a FTP upload folder). The following solutions I found useful to fulfill the above requirements:

jNotify

I’ve got a problem when I tried to run it on a Ubuntu 64bit machine, but after searching on their forum on sourceforge, I’ve found my solutions (by recompiling the native code). I’ve used it in production since then and it never failed me. But there is a catch: when you move files/folders into your monitored folders, it won’t detect the event as new files/folders created. I think it’s because Linux only updates (and not creates) the inodes of those files/folders on the ext4 filesystem. In order for your Java application to pickup the native code, you have to put the native library (.so for Linux, .dll for Windows) in the folder specified by the system property java.library.path

JPathWatch

WatchService in JDK 7

You can download all the sample code at https://bitbucket.org/tinhtruong/blog-samples/src/6d3ce2ac58a8/filesystem-monitoring