• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Urgent!Why the field of java object passed to native method becomes 0? (JNI Question)

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, All,
If any of you know the reason, please let me know. Your help will be greatly appreciated.
I am writing some JNI application, and find the field value of the java object passed to native method all becomes 0 in the native method, and I can not change the field of the object passed to the native method in the native method. I attached all the code in the following and the run result, please have a look at it, and let me know why?
The platform is PIV PC with Fedora Core 1 installed. And JDK is j2sdk1.4.2_04 from SUN.
-----------------------------
/*The Java Code*/
/*
* noa.java
*
* Created on March 28, 2004, 8:18 PM
*/
/**
*
* @author gechen
*/
public class noa {

public int iv;
public double dv;

/** Creates a new instance of noa */
/*
static{
System.load("/home/gechen/mytests/noa/class/libnoa.so");
}
*/

public native void callnative(noadc obja, noadc objb, int intv);

public noa() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
noa obja = new noa();

noa objb = new noa();

//System.out.println(
obja.run();
}

public void run(){

noadc objin = new noadc();
noadc objout = new noadc();

objin.iv = 1;
objin.dv = 2.2;

System.load("/home/gechen/mytests/noa/class/libnoa.so");
System.out.println("Before Entering native call: objin iv="+objin.iv+" dv ="+objin.dv);
callnative(objin, objout, objin.iv);
System.out.println("After Return from native call: objin iv="+objin.iv+" dv ="+objin.dv);
System.out.println("After Return from native call: objout iv="+objout.iv+" dv ="+objout.dv);

}


}

/*
* noadc.java
*
* Created on March 28, 2004, 8:28 PM
*/
/**
*
* @author gechen
*/
public class noadc {

public int iv;

public double dv;

/** Creates a new instance of noadc */
public noadc() {
//this.iv = 0;
//this.dv = 0.0;
//System.out.println("In noadc()");
}


}
-----------------------------------------------
------------------------------------------------
/*The C code*/
#include <stdio.h>
#include "noa.h"
JNIEXPORT void JNICALL Java_noa_callnative
(JNIEnv *env, jobject to, jobject pobjin, jobject pobjout, jint piv)
{
jclass objin = (*env)->GetObjectClass(env, pobjin);

jfieldID fid_iv = (*env)->GetFieldID(env, objin, "iv", "I");
jint iv = (*env)->GetIntField(env, objin, fid_iv);
if (fid_iv == 0){
printf("fid_iv == 0, error return\n");
return;
}
jfieldID fid_dv = (*env)->GetFieldID(env, objin, "dv", "D");
jdouble dv = (*env)->GetDoubleField(env, objin, fid_dv);
printf("<Inside Native>: pobjin.iv=%d dv=%f piv=%d\n", iv, dv, piv);
jclass objout = (*env)->GetObjectClass(env, pobjout);
fid_iv = (*env)->GetFieldID(env, objout, "iv", "I");
(*env)->SetIntField(env, objout, fid_iv, 2);
iv = (*env)->GetIntField(env, objout, fid_iv);
printf("<Inside Native>: After SetIntField of objout.iv: objout.iv=%d\n",iv);
return;

}
-----------------------------------------------------------------
------------------------------------------------------------------
/*The execution result*/
Before Entering native call: objin iv=1 dv =2.2
<Inside Native>: pobjin.iv=0 dv=0.000000 piv=1
<Inside Native>: After SetIntField of objout.iv: objout.iv=2
After Return from native call: objin iv=1 dv =2.2
After Return from native call: objout iv=0 dv =0.0
---------------------------------------------------------------------
Thanks a lot. You help will be greatly appreciated.
--Robin
 
I can't renounce my name. It's on all my stationery! And hinted in this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic