It is very simple to use, taking my ubuntu system as an example:
1. Introduce the jnotify package into the project.
2. Add the so file that jnotify depends on to the java.library.path path. This variable may have multiple locations, just add the libjnotify.so file included in the jnotify archive to any of the paths. If you don't know the value of this variable, you can use System.getProperty("java.library.path") to check. Of course, if you don't want to be so troublesome, you can specify the JVM parameter
Java code
1.-Djava.library.path=your location when starting the program,
which has the same effect as adding the so file to the system path above.
Then, write a test class to see the effect.
I didn't write it myself here, but simply copied the test code from JNotify's official website.
Java code
1.
public class JnotifyTest { 2. public static void main(String[] args) { 3. try { 4. new JnotifyTest().sample(); 5. } catch (Exception e) { 6. e.printStackTrace (); 7. } 8. // System.out.println(System.getProperty("java.library.path")); 9. } 10. 11. public void sample() throws Exception { 12. // path to watch 13. String path = System.getProperty("user.home"); 14. 15. // watch mask, specify events you care about, 16. // or JNotify.FILE_ANY for all events. 17. int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED 18. | JNotify.FILE_MODIFIED | JNotify.FILE_RENAMED; 19. 20. // watch subtree? 21. boolean watchSubtree = true; 22. 23. // add actual watch 24. int watchID = JNotify 25. .addWatch(path, mask, watchSubtree, new Listener()); 26. 27. // sleep a little, the application will exit if you 28. // don't (watching is asynchronous), depending on your 29. // application, this may not be required 30. Thread.sleep(1000000); 31. 32. // to remove watch the watch 33. boolean res = JNotify.removeWatch(watchID); 34. if (!res) { 35. // invalid watch ID specified. 36. } 37. } 38.
39. //You can add your own code in the following monitoring methods.
For example, add code 40 to reload the configuration file in fileModified .
class Listener implements JNotifyListener { 41. public void fileRenamed(int wd, String rootPath, String oldName, 42. String newName) { 43. print("renamed " + rootPath + " : " + oldName + " -> " + newName); 44. } 45. 46. public void fileModified(int wd, String rootPath, String name) { 47. print("modified " + rootPath + " : " + name); 48. } 49. 50. public void fileDeleted(int wd, String rootPath, String name) { 51. print("deleted " + rootPath + " : " + name); 52. } 53. 54. public void fileCreated(int wd, String rootPath, String name) { 55. print("created " + rootPath + " : " + name); 56. } 57. 58. void print(String msg) { 59. System.err.println(msg); 60. } 61. 62. } 63.}
In the actual use process, if it is a web project, my habit is to add a listener listener, and when the listener is initialized, add monitoring of the specified file or folder. In this way, we don't have to worry about restarting the project every time we modify the configuration file. If it is a Java project, add monitoring where needed.
Reprinted from: http://lichuanbao.iteye.com/blog/1532951