0

I'm newbie of JNI so I dont know how to getting a string array, I read a lot of question in stackoverflow and also read JNI Programer Guide book but it's difficult for me :( after a long time, I came up with this code but it didn't work

class ReadString2 {
    private int i;
    private static String[] StringBuf;
    private native void readS(String[] StringBuf);

    /*Constructor of ReadString*/
    public ReadString2() {
       // StringBuf = "This is exercise number 1";
      StringBuf = new String[5];
      StringBuf[0] = new String("123");
      StringBuf[1] = new String("456");
      StringBuf[2] = new String("789");
      StringBuf[3] = new String("\0");
      StringBuf[4] = new String("\0");
    }
    public static void main(String[] args) {
        ReadString2 p = new ReadString2();

        p.readS(StringBuf);
    }
    static {
        System.loadLibrary("ReadString2");
    }
}

//===================C==================

#include <jni.h>
#include <stdio.h>
#include "ReadString2.h"

JNIEXPORT void JNICALL
Java_ReadString2_ReadS(JNIEnv *env,jobject obj, jobjectArray StringBuf)
{

    jsize  stringCount = (*env)->GetArrayLength(env, StringBuf);
    const jbyte *str;
    jstring stringGet;
    int i;


    if (str == NULL) {
        //return NULL;
    }
    for (i = 0; i <= stringCount; i++) {
        stringGet = (*env)->GetObjectArrayElement(env, StringBuf, i);
        str = (*env)->GetStringUTFChars(env, stringGet, NULL);
        printf("%s\n",str);
    }
    (*env)->ReleaseStringUTFChars(env, StringBuf, str);


}

I built ReadString2.h file by

 javah -jni ReadString2 

ReadString2.class by

javac ReadString2.java

ReadString2.so file by

 cc -I"/usr/lib/jvm/java/include/" -I"/usr/lib/jvm/java/include/linux/" ReadString2.c -o libReadString2.so -shared

and run by

java -Djava.library.path=. ReadString2

The error is:

Exception in thread "main" java.lang.UnsatisfiedLinkError: ReadString2.readS([Ljava/lang/String;)V
        at ReadString2.readS(Native Method)
        at ReadString2.main(ReadString2.java:19)

SO, what's wrong with my code? I have another question is that how can I Throw NullPointerException if any item is null??

Sang.Mai
  • 1
  • 3

2 Answers2

0

ReadString2.dll or ReadString2.so can not be found. Under Windows you may put the dll in the "current" directory or - more generally - you should specify the library path to the JVM with -Djava.library.path=...

How to set the java.library.path from Eclipse

In your code you have to release the resource with:

void ReleaseStringChars(JNIEnv *env, jstring string, const jchar *chars);

Please note that

javah -jni ReadString2 

does not build ReadString2.so but only ReadString2.h.

Under Linux, and I don't know why, export LD_LIBRARY_PATH=... is needed too.

Community
  • 1
  • 1
Aerospace
  • 1,270
  • 12
  • 19
  • Thanks for reply, I have already done as you said, but it did not work at all. I use Linux system. Buiding ReadString2.So file by javah -jni ReadString2 and run by java -Djava.library.path=. ReadString2. – Sang.Mai Feb 04 '13 at 09:40
0

Finally I found the problem of my code. This is my mistake, sorry guys. I used:

Java_ReadString2_ReadS(JNIEnv *env,jobject obj, jobjectArray StringBuf)"{}

instead of

Java_ReadString2_readS(JNIEnv *env,jobject obj, jobjectArray StringBuf)"{}

ReadS->readS @@

Sang.Mai
  • 1
  • 3