20

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.

2
  • Thanks very much for the help, your tips were all correct, my lack of reputation restricts me from noting them all as helpful. Cheers Commented May 28, 2009 at 12:33
  • please follow this link for more detail saurabhsharma123k.blogspot.in/2017/07/… Commented Jul 4, 2017 at 15:52

5 Answers 5

24

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The link is alright, but the note about using '-rpath' is bogus. For 'rpath to work, it would have to be specified when compiling that java virtual machine itself.
18

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.

Comments

7

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

Comments

5

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.

1 Comment

And if the native library is accessed via JNA (not JNI), you must instead define jna.library.path .
3

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.