1

我一直在尝试在 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)

我已按照其中一篇博客的说明将路径设置为 libjnotify.so,但我仍然收到此错误 PATH 设置如下:

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

1 回答 1

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
于 2013-12-06T11:19:43.173 回答