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
Solved

setting java objects from C++ (JNI)

Posted on 1997-03-25
1
1,256 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
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

Title # Comments Views Activity
more than one jdk and one jre 1 50
java stored proc example 9 44
ejb stateless example 2 20
mysql jsp example issue 32 48
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.

840 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