I have a problem to set a string from jni to java class,i wrote the jni (in c) , I want to set a string from jni to java i did like this
jclass cls;
jmethodID mid;
/* cls = (*env)->GetObjectClass(env, obj); */
cls = (*env)->FindClass (env,"com/example/lsextractor/LSCore");
jobject objRet = (*env)->AllocObject(env,cls);
jstring estr = (jstring)(*env)->NewStringUTF(env,(char*)"Hello");
mid = (*env)->GetMethodID(env, cls, "setTemplate","(Ljava/lang/String;)V");
(*env)->CallObjectMethod(env,objRet,mid,estr);
return (jint)1;
but this method is not working i cant get the string from my class but i can able to access all other method from my java class i can't set the string from jni to java do have any idea to do this, this is my class
public byte[] getimage()
{
return this.Image;
}
public void setimage(byte[] rawImg)
{
this.Image=rawImg;
}
public String getTemplate()
{
return this.Template;
}
public void setTemplate(String Tmp)
{
this.Template = Tmp;
}
static
{
System.loadLibrary("test");
}
First you should not be using AllocObject
since no constructor will be called with that function.
Try this instead:
mid = (*env)->GetMethodID(env, cls, "<init>", "()V");
jobject objRet = (*env)->NewObject(env, cls, mid);
That will give you an initialized object.
Second problem is that you are calling CallObjectMethod
The correct method invocation is:
(*env)->CallVoidMethod(env, objRet, mid, estr);
That is because the Void
part of the function is the return type. Your call tried to call a method that returns Object
.
AllocObject
jobject AllocObject(JNIEnv *env, jclass clazz);
Allocates a new Java object without invoking any of the constructors for the object. Returns a reference to the object.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.