6

java.lang.UnsatisfiedLinkError

I'm using the hello-jni example, and for whatever reason, I'm getting a java.lang.UnsatisfiedLinkError when I try to call the hello-jni library. Any ideas why? Do I have to set my path somewhere?

in HelloJni.java:

public native String stringFromJNI(); 

and

static { System.loadLibrary("hello-jni"); } 

in hello-jni.c:

jstring Java_com_bdunlay_hellojni_HelloJni_stringFromJNI( JNIEnv* env, jobject thiz ) { return (*env)->NewStringUTF(env, "Hello from JNI !"); } 

exception trace

the .so file is... project_root/libs/armeabi/libhello-jni.so

5
  • Information is too few. where is the .so you put and what is the filename ? What is the argument of your System.loadLibrary() Commented Jan 27, 2011 at 6:44
  • 1
    Hmmm. It looks good. Please show the result of 'arm-eabi-nm libhello-jni.so' and header file as well. Commented Jan 27, 2011 at 7:03
  • arm-eabi-nm libhello-jni.so: no symbols. Haha, embarrassing. I didn't have a header file in my project! I'd give you checkmark if you'd answer make sure you include your header files! Commented Jan 27, 2011 at 7:08
  • Do I need to include the jni.h file in every project that uses the NDK? Commented Jan 27, 2011 at 7:11
  • you should generate the header from java class properly. Commented Jan 27, 2011 at 7:16

4 Answers 4

6

your native have no JNIEXPORT. It usually declares in header file with function declaration.

We will use javah -jni to generate the header

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

7 Comments

This may be a trivial question, but how do I manually generate this? I am writing this through eclipse and pressing the play button to compile everything the easy way :)
javah -jni YourClassWithFullName
in root/src/com/bdunlay/hellojni I ran javah -jni HelloJni.java and got this response: $ javah -jni HelloJni.java / error: cannot access HelloJni.java / class file for HelloJni.java not found / javadoc: error - Class HelloJni.java not found. / Error: No classes were specified on the command line. Try -help. / I also ran it without .java and it said that the class was not found as well.
javah will parse the bytecode, not source code. You need to compile the HelloJni.java to HelloJni.class first.
|
5

See android-ndk-r8b/documentation.html for additional details.

By default, the sample does not include an Application.mk file (in the /jni/ folder). I corrected the problem by adding this file to my project and adding the following single entry which allows the built files to build for multiple CPU types (specifically looking for x86 in my case):

APP_ABI := armeabi armeabi-v7a x86 

After adding that file, you can again run ndk-build to produce the required files and then build your APK as normal.

Comments

1

java.lang.UnsatisfiedLinkError: Native method not found exception for methods from OpenCV means that you try to use OpenCV before its initialization. You may use OpenCV objects and call methods from library only after onManagerConnected with status LoaderCallbackInterface.SUCCESS.

Comments

1

In my case the reason of error was: If you have multiple libraries loaded as

System.loadLibrary("lib1"); System.loadLibrary("lib2"); 

and lib1 depends on lib2, you need load lib2 first.

1 Comment

even better: ndk-depends from Android SDK may be used to get right order of loading your dynamic libraries. Just type ndk-depends --print-java <path to your so file>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.