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??