deepak
deepak

Reputation: 317

Exception rised when runing jnotify program in linux

I have been trying to run JNotify sample code on Fedora16 the code is as follows:

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

This gives me an exception as follows :

/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)

I have Set PATH to libjnotify.so as instructed in one of the blog , but still i get this error PATH set by me is as follows :

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

Upvotes: 1

Views: 2986

Answers (1)

binarytemple_picsolve
binarytemple_picsolve

Reputation: 2666

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

The environmental variable name is platform specific

On Windows you set PATH

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

On Linux you set LD_LIBRARY_PATH

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

On OSX you set DYLD_LIBRARY_PATH

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

To avoid this issue, it is much better to use the Java command flag instead, as it is platform agnostic, i.e.

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

Upvotes: 1

Related Questions