java - 在Linux中运行jnotify程序时引发异常

标签 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();
    }
}

这给了我一个异常(exception),如下:

/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,但仍然收到此错误 我设置的PATH如下:

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

最佳答案

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

关于java - 在Linux中运行jnotify程序时引发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17646624/

相关文章:

tomcat - 在 Fedora 16 (Verne) 中启动 Apache Tomcat

java - Maven 网址太长

java - GCM android 接收广播 Intent 时出错 { act=PATH.DISPLAY_MESSAGE flg=0x10 (有额外内容)

java - 如何从应用程序中的每个文件中删除特定的导入?

eclipse - 自定义 Eclipse 包

java - Eclipse 插件无法与 GlassFish 服务器通信,请检查防病毒软件是否阻止或监控此端口

java - 在 Fedora 16 中为 Libre Office Base 设置 java 类路径

java - 使用 JGit 检索最旧的提交

java - 可在始终为空的情况下调用

git - 致命 : Interactive git shell is not enabled. 收到致命消息:交互式 git shell 未启用