gav
gav

Reputation: 29122

How should I load native libraries for JNI to avoid an UnsatisfiedLinkError?

I want to use JNI on Ubuntu 8.10, using Eclipse and gcc (the standard one with Ubuntu if there are flavours).

I can't seem to load my library despite the make file creating it successfully.

The main Java class is as follows:

class Hello {
    public native void sayHello();

    static {
        System.loadLibrary("hello.so");
    }

    public static void main(String[] args) {
        Hello h = new Hello();
        h.sayHello();
    }
}

My make file is as such;

    all : hello.so

hello.so : Hello.o
    gcc -shared -o hello.so Hello.o

Hello.o : Hello.c Hello.h
    gcc -I/usr/lib/jvm/java-6-sun/include -I/usr/lib/jvm/java-6-sun/include/linux -c Hello.c -o Hello.o

Hello.h : Hello.class
    javah -jni Hello

clean :
    -del Hello.h
    -del Hello.o

The rest of the code (Hello.c) looks like one would think.

The error I'm getting is as follows;

Exception in thread "main" java.lang.UnsatisfiedLinkError: no hello.so in java.library.path

If I use an explicit path:

System.loadLibrary("/home/gavin/Work/workspace/JNI/hello.so");

Then it works, but I'd much rather not use an explicit path if possible.

Upvotes: 20

Views: 64409

Answers (5)

echoandlove
echoandlove

Reputation: 31

OS: CentOS6.5. JNIHello.java:

public class JNIHello {
                static {
                                System.loadLibrary("JNIHello");
                }
                private native void sayHello();
                public static void main(String args[]) {
                                JNIHello jniHello = new JNIHello();
                                jniHello.sayHello();
                }
}

export java home: export JAVA_HOME=/usr/java/jdk1.7.0_67-cloudera/

compile java class:

javac JNIHello.java

generate JNIHello.h:

javah JNIHello

implement sayHello in JNIHello.c:

#include <jni.h>
#include <stdio.h>
#include "JNIHello.h"
/*
 * Class:     JNIHello
 * Method:    sayHello
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_JNIHello_sayHello
  (JNIEnv *env, jobject obj) {
        printf("Hello world!\n");
        return;
}

compile library:

gcc -I"$JAVA_HOME/include" -I"$JAVA_HOME/include/linux" JNIHello.c -fPIC -shared -o JNIHello.so

run JNIHello:

java -Djava.library.path=/home/ldp/caffe/test/ JNIHello
Hello world!

lib name format ref:

3.1.1. Shared Library Names

Every shared library has a special name called the ``soname''. The soname has the prefix 'lib', the name of the library, the phrase '.so',

ref link

Upvotes: 3

Nick
Nick

Reputation: 71

Do the following:

  1. change your Java class to this:

    class Hello {
    
        public native void sayHello();
    
        static {
            System.loadLibrary("hello");
        }
    
        public static void main(String[] args) {
            Hello h = new Hello();
            h.sayHello();
        }
    }
    
  2. rename hello.so to libhello.so: cp hello.so libhello.so or mv hello.so libhello.so

  3. run as: java -Djava.library.path=/home/gavin/Work/workspace/JNI/ Hello

Upvotes: 7

Daniel Nesbitt
Daniel Nesbitt

Reputation: 780

As per Pax you should set the library path to where ever Java should look for the library. Your library name should be libhello.so. The call to load the library should then be:

System.loadLibrary("hello");

Linux libraries are referenced by the convention libname.so and loaded based on the name. Here is a link about dynamic linking problems in Java from the SWIG documentation, although you are not using SWIG this section is still relevant.

Upvotes: 24

JesperE
JesperE

Reputation: 64444

You're calling System.loadLibrary() incorrect. The loadLibrary method takes a library name, e.g. "hello", and tries to load the corresponding shared object. On Unix, it will attempt to load "libhello.so", and on windows it will try to load "hello.dll". It will expect the file to be found in java.library.path.

The method you probably intend to be calling is System.load() which takes a fully qualified filename and loads it. This method should take a File as argument, but it takes a string instead. If you use load, you'll have to handle local naming conventions manually, but you won't have to rely on java.library.path to be set.

Upvotes: 18

paxdiablo
paxdiablo

Reputation: 882326

And are you running it with something like:

java -Djava.library.path=/home/gavin/Work/workspace/JNI Hello

You'll need to make sure the shared object is in your library path.

Upvotes: 5

Related Questions