rhe
asked on
setting java objects from C++ (JNI)
I can't set a NON static member in java from a native C++ function. It works fine with STATIC.
I'm wrong or is it a bug in JNI ?
-------------------------- ---------- ---------- ----
import java.io.*;
public class SimpleFile {
public String fd=new String("-1");
public native boolean set();
static {
System.loadLibrary("ipi");
}
static public void main(String[] args) throws IOException {
System.out.println("Simple File.class :->:main") ;
SimpleFile f = new SimpleFile();
f.set();
System.out.println(f.fd);
System.out.println("Simple File.class :<-:main") ;
}
}
-------------------------- ---------- ---------- ----
#include "SimpleFile.h"
JNIEXPORT jboolean JNICALL Java_SimpleFile_set
(JNIEnv * env, jobject jobj)
{
jclass jcl;
jfieldID jfid;
jstring jstr;
if(!(jcl=env->GetObjectCla ss(jobj)))
return JNI_FALSE;
jfid=env->GetFieldID(jcl," fd","Ljava /lang/Stri ng;");
if(!jfid)
return JNI_FALSE;
jstr=(jstring)(env->NewStr ingUTF("12 3"));
env->SetObjectField(jcl,jf id,(jobjec t)jstr);
return JNI_TRUE;
}
-------------------------- ---------- ---------- ----
I'm wrong or is it a bug in JNI ?
--------------------------
import java.io.*;
public class SimpleFile {
public String fd=new String("-1");
public native boolean set();
static {
System.loadLibrary("ipi");
}
static public void main(String[] args) throws IOException {
System.out.println("Simple
SimpleFile f = new SimpleFile();
f.set();
System.out.println(f.fd);
System.out.println("Simple
}
}
--------------------------
#include "SimpleFile.h"
JNIEXPORT jboolean JNICALL Java_SimpleFile_set
(JNIEnv * env, jobject jobj)
{
jclass jcl;
jfieldID jfid;
jstring jstr;
if(!(jcl=env->GetObjectCla
return JNI_FALSE;
jfid=env->GetFieldID(jcl,"
if(!jfid)
return JNI_FALSE;
jstr=(jstring)(env->NewStr
env->SetObjectField(jcl,jf
return JNI_TRUE;
}
--------------------------
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.