Link to home
Start Free TrialLog in
Avatar of rhe
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("SimpleFile.class:->:main");
        SimpleFile f = new SimpleFile();
        f.set();
        System.out.println(f.fd);
        System.out.println("SimpleFile.class:<-:main");
    }
}

--------------------------------------------------
#include "SimpleFile.h"
 
JNIEXPORT jboolean JNICALL Java_SimpleFile_set
  (JNIEnv * env, jobject jobj)
{
        jclass jcl;
        jfieldID jfid;
        jstring jstr;

        if(!(jcl=env->GetObjectClass(jobj)))
                return JNI_FALSE;

        jfid=env->GetFieldID(jcl,"fd","Ljava/lang/String;");
        if(!jfid)
                return JNI_FALSE;

        jstr=(jstring)(env->NewStringUTF("123"));
        env->SetObjectField(jcl,jfid,(jobject)jstr);

        return JNI_TRUE;
}

--------------------------------------------------

ASKER CERTIFIED SOLUTION
Avatar of bbabbrah
bbabbrah

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial