Solved

setting java objects from C++ (JNI)

Posted on 1997-03-25
1
1,170 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
1 Comment
 

Accepted Solution

by:
bbabbrah earned 50 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now