4 Replies Latest reply: Jun 4, 2012 7:53 PM by jschellSomeoneStoleMyAlias RSS

    Strugging getting HelloWorld running -- UnsatisfiedLinkError

    940666
      We are following the instructions from this URL:

      http://www.ibm.com/developerworks/java/tutorials/j-jni/section2.html

      We are using C, not C++.

      Just pasting this here so you guys can see the files we are using -- We have just copied the files over without changing these, then javac'ed the java file, and ran java.h on it to generate Sample1.h

      Then we compile the .so file like this:

      gcc -g -I/usr/java6/include Sample1.c -o libSample1.so

      (/usr/java6/include is the path to where jni.h lives on this system)

      And then we run the java file like this:

      java -Djava.library.path=/my/home/directory Sample1

      ( where /my/home/directory is where Sample1.java, Sample1.h, and Samle1.c live)


      It is now making it past the System.loadLibrary("Sample1") step but is bombing like this when it tries to call the native methods:

      Exception in thread "main" java.lang.UnsatisfiedLinkError: Sample1.intMethod(I) I

      Does anyone know what we could be doing wrong here?

      Thanks!!,

      Jim
      For completeness I'll just include the files here (we took out the line numbers before compiling! : )


      Sample1.java
      ================
      1. public class Sample1
      2. {
      3. public native int intMethod(int n);
      4. public native boolean booleanMethod(boolean bool);
      5. public native String stringMethod(String text);
      6. public native int intArrayMethod(int[] intArray);
      7.
      8. public static void main(String[] args)
      9. {
      10. System.loadLibrary("Sample1");
      11. Sample1 sample = new Sample1();
      12. int square = sample.intMethod(5);
      13. boolean bool = sample.booleanMethod(true);
      14. String text = sample.stringMethod("JAVA");
      15. int sum = sample.intArrayMethod(
      16. new int[]{1,1,2,3,5,8,13} );
      17.
      18. System.out.println("intMethod: " + square);
      19. System.out.println("booleanMethod: " + bool);
      20. System.out.println("stringMethod: " + text);
      21. System.out.println("intArrayMethod: " + sum);
      22. }
      23. }



      Generated Sample1.h file from javah
      ==========================
      /* DO NOT EDIT THIS FILE - it is machine generated */
      #include <jni.h>
      /* Header for class Sample1 */

      #ifndef IncludedSample1
      #define IncludedSample1
      #ifdef __cplusplus
      extern "C" {
      #endif
      /*
      * Class: Sample1
      * Method: intMethod
      * Signature: (I)I
      */
      JNIEXPORT jint JNICALL Java_Sample1_intMethod
      (JNIEnv *, jobject, jint);

      /*
      * Class: Sample1
      * Method: booleanMethod
      * Signature: (Z)Z
      */
      JNIEXPORT jboolean JNICALL Java_Sample1_booleanMethod
      (JNIEnv *, jobject, jboolean);

      /*
      * Class: Sample1
      * Method: stringMethod
      * Signature: (Ljava/lang/String;)Ljava/lang/String;
      */
      JNIEXPORT jstring JNICALL Java_Sample1_stringMethod
      (JNIEnv *, jobject, jstring);

      /*
      * Class: Sample1
      * Method: intArrayMethod
      * Signature: ([I)I
      */
      JNIEXPORT jint JNICALL Java_Sample1_intArrayMethod
      (JNIEnv *, jobject, jintArray);

      #ifdef __cplusplus
      }
      #endif
      #endif



      Sample1.c
      =======
      1. #include "Sample1.h"
      2. #include <string.h>
      3.
      4. JNIEXPORT jint JNICALL Java_Sample1_intMethod
      5. (JNIEnv *env, jobject obj, jint num) {
      6. return num * num;
      7. }
      8.
      9. JNIEXPORT jboolean JNICALL Java_Sample1_booleanMethod
      10. (JNIEnv *env, jobject obj, jboolean boolean) {
      11. return !boolean;
      12. }
      13.
      14. JNIEXPORT jstring JNICALL Java_Sample1_stringMethod
      15. (JNIEnv *env, jobject obj, jstring string) {
      16. const char str = (env)->GetStringUTFChars(env, string, 0);
      17. char cap[128];
      18. strcpy(cap, str);
      19. (*env)->ReleaseStringUTFChars(env, string, str);
      20. return (*env)->NewStringUTF(env, strupr(cap));
      21. }
      22.
      23. JNIEXPORT jint JNICALL Java_Sample1_intArrayMethod
      24. (JNIEnv *env, jobject obj, jintArray array) {
      25. int i, sum = 0;
      26. jsize len = (*env)->GetArrayLength(env, array);
      27. jint body = (env)->GetIntArrayElements(env, array, 0);
      28. for (i=0; i<len; i++)
      29. {     sum += body[i];
      30. }
      31. (*env)->ReleaseIntArrayElements(env, array, body, 0);
      32. return sum;
      33. }
      34.
      35. void main(){}