在linux中运行jnotify程序时引发异常

时间:2013-07-15 04:07:38

标签: java eclipse fedora16 jnotify

我一直在尝试在Fedora16上运行JNotify示例代码,代码如下:

import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyListener;
import net.contentobjects.jnotify.linux.JNotify_linux;
public class JNotifyDemo {

public void sample() throws Exception {
        // path to watch
        String path = System.getProperty("user.home");
        System.out.println(path);
        // watch mask, specify events you care about,
        // or JNotify.FILE_ANY for all events.
        int mask = JNotify_linux.IN_CREATE
                | JNotify_linux.IN_DELETE
                | JNotify_linux.IN_MODIFY
                | JNotify_linux.IN_ATTRIB;

        // watch subtree?
        boolean watchSubtree = true;

        // add actual watch
        int watchID = JNotify.addWatch(path, mask, watchSubtree, new Listener());

        // sleep a little, the application will exit if you
        // don't (watching is asynchronous), depending on your
        // application, this may not be required
        Thread.sleep(1000000);

        // to remove watch the watch
        // boolean res = JNotify.removeWatch(watchID);
        // if (!res) {
        // 
        // }
    }

    class Listener implements JNotifyListener {

        public void fileRenamed(int wd, String rootPath, String oldName,
                String newName) {
            print("renamed " + rootPath + " : " + oldName + " -> " + newName);
        }

        public void fileModified(int wd, String rootPath, String name) {
            print("modified " + rootPath + " : " + name);
        }

        public void fileDeleted(int wd, String rootPath, String name) {
            print("deleted " + rootPath + " : " + name);
        }

        public void fileCreated(int wd, String rootPath, String name) {
            print("created " + rootPath + " : " + name);
        }

        void print(String msg) {
            System.err.println(msg);
        }
    }

    public static void main(String[] args) throws Exception {
        new JNotifyDemo().sample();
    }
}

这给了我一个例外如下:

/home/student Exception in thread "main" java.lang.UnsatisfiedLinkError: no jnotify in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1681)
at java.lang.Runtime.loadLibrary0(Runtime.java:840)
at java.lang.System.loadLibrary(System.java:1047)
at net.contentobjects.jnotify.linux.JNotify_linux.<clinit>(Unknown Source)
at net.contentobjects.jnotify.linux.JNotifyAdapterLinux.<init>(Unknown Source)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:532)
at java.lang.Class.newInstance0(Class.java:374)
at java.lang.Class.newInstance(Class.java:327)
at net.contentobjects.jnotify.JNotify.<clinit>(Unknown Source)
at test.JNotifyDemo.sample(JNotifyDemo.java:29)
at test.JNotifyDemo.main(JNotifyDemo.java:67)

我按照其中一个博客的指示将PATH设置为libjnotify.so但仍然出现此错误 我设置的路径如下:

export PATH=${PATH}:/home/student/workspace/Google_Project/jnotify-lib-0.94/64-bit_Linux/

1 个答案:

答案 0 :(得分:1)

http://www.coderanch.com/t/377174/java/java/java-library-path

环境变量名称是特定于平台的

在Windows上设置PATH

(don't actually remember how to set a windows env variable)    

在Linux上设置LD_LIBRARY_PATH

export LD_LIBRARY_PATH=jnotify-lib-0.94/64-bit_Linux/ 

在OSX上设置DYLD_LIBRARY_PATH

export DYLD_LIBRARY_PATH=/common/bryan_scratch/poc/poc_vips/project/src/.libs

为避免此问题,最好使用Java命令标志,因为它与平台无关,即

java -Djava.library.path=jnotify-lib-0.94/64-bit_Linux org.blah.MyMain