Solved

setting java objects from C++ (JNI)

Posted on 1997-03-25
1
1,321 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 50 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

707 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