?
Solved

setting java objects from C++ (JNI)

Posted on 1997-03-25
1
Medium Priority
?
1,396 Views
Last Modified: 2013-11-23
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;
}

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

0
Comment
Question by:rhe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 

Accepted Solution

by:
bbabbrah earned 100 total points
ID: 1219202
jstr=(jstring)(env->NewStringUTF("123"));
                          env->SetObjectField(jcl,jfid,(jobject)jstr);

The above line should be

env->SetObjectField(obj, jfid, (object)jstr);
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month7 days, 19 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question