I want to create a listener to detect directory change recursively (the main directory and its subdirectories). After a web search I found this link that explains the use of the class WatchService from the Package java.nio.file that api exactly meets my needs but unfortunately it is only available in Java 7! Then I return to search again in order to find a framework that allows the same and is compatible java 5 and java 6 but again there was a problem, because recursion is available for Windows and my application should use Linux!! Can you offer me a solution: another framework, a way to do..
-
If the framework you linked works for one file , then making it work recusively for subfolders should not be a *major* problem , or is it ? – Bhaskar Sep 23 '11 at 09:35
-
Why are people always scared to upgrade to the newest Java Version? – Angel O'Sphere Sep 23 '11 at 09:43
-
Because of the compiler optimisation bug released with Java 7? http://stackoverflow.com/questions/6894104/how-serious-is-the-java7-solr-lucene-bug – Adrian Cox Sep 23 '11 at 09:49
-
possible duplicate of [Notify a Button if the number of files in a directory change](http://stackoverflow.com/questions/1256012/notify-a-button-if-the-number-of-files-in-a-directory-change) – McDowell Sep 23 '11 at 10:13
-
@AdrianCox: that's not really a valid reason. JVM runtimes occasionally have bugs, even "stable" ones. And that particular one will soon be fixed. – Joachim Sauer Sep 23 '11 at 11:52
4 Answers
I think you did a good discovery job and found a wonderful library jpathwatch. I do not understand what was your problem with recursion: I have not seen any restriction for linux in this library documentation.
But if for some reason jpathwatch cannot help you on linux, it is not a problem: you can run du command yourself. See this reference: http://linux.about.com/library/cmd/blcmdl1_du.htm
If I were you I probably do the following: write simple script that runs du in loop. Then run this script from java from separate thread that is contiguously reading the script's output and analyses it.

- 114,158
- 16
- 130
- 208
-
http://jpathwatch.wordpress.com/documentation/features/ that link explains that watching change in sub directories is only available for windows – user405458 Sep 23 '11 at 09:45
-
Running `du` will cause a large amount of disk IO, and won't detect changes that don't modify file size. – Adrian Cox Sep 23 '11 at 09:49
-
That's right. If file size is not changed, du does not help. I just wanted to offer work-around. – AlexR Sep 23 '11 at 09:55
This is a kind of functionality that requires support of JVM or a native library, such as the one you've found for Windows. If you can't find anything in Java for Linux, I suggest asking for a binary Linux library (in a different question) and then build a Java native class on top of that.
I hope other people will help you better.

- 10,511
- 9
- 46
- 84
To do this on Linux you need to use Java 7, or a native library that uses inotify
. Have you considered the JNotify library? It looks like it handles recursion into subdirectories, including newly created ones.

- 6,204
- 5
- 41
- 68
Have a look at http://download.oracle.com/javase/tutorial/essential/io/notification.html
"The java.nio.file package provides a file change notification API, called the Watch Service API. This API enables you to register a directory (or directories) with the watch service. When registering, you tell the service which types of events you are interested in: file creation, file deletion, or file modification. When the service detects an event of interest, it is forwarded to the registered process. The registered process has a thread (or a pool of threads) dedicated to watching for any events it has registered for. When an event comes in, it is handled as needed."
Update: Ooops, just realized you already saw this. I didn't realize this was only in Java 7 :-(
-
Ooops, just realized you already saw this. I didn't realize this was only in Java 7 :-( – Josh Bjornson Sep 27 '11 at 09:16