Help coderanch get a
new server
by contributing to the fundraiser
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JNI and Objects as Params

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings...
I am having some problems with JNI. I have written some pseudo code to better explain my problem. Basically I have an object in Java that I pass into the native as an argument. In the native code I want to use the values of the variables encapsulated in that object. The thing is I assign the values on the Java side of things, but when they get into the C side of things, the values associated with the variables are zero. I thought it was a pointer type issue, but played around with that a lot in the native, and I'm 99% sure that's not it. If I ran the following code I would get the following output:
Val in Java: 13
MidVal: 0 FidVal: 0
Any insight is greatly appreciated.
//The Java Class
package MyPackage;
imports...
public Class NativeCaller{

public static void main(String[] args){
NativeCaller lCaller = new NativeCaller();
AClass lArgument = new AClass;
lArgument.setVal(13);//a simple setter
System.out.println("Val in Java: " + lArgument.getVal());//simple getter
lCaller.MyMethod(lArgument);
}

public synchronized native Object MyMethod(AClass inArg);
}
//The C Function
JNIEXPORT jobject JNICALL Java_SomeProject_MyPackage_MyClass_MyMethod(
JNIEnv *env, jobject jthis, jobject inObject){
jobject result = (jobject)NULL;
jclass lCls;
jmethodID jmid;
jfieldID jfid;
double lMidVal;
double lFidVal;
lCls = (*env)->FindClass(env, "SomeProject/MyPackage/AClass");
if (lCls == NULL){
printf("Error getting class");
return (jobject)NULL;
}

if (!(*env)->IsInstanceOf(env, inObject, lCls)){
printf("input object is not correct type");
return (jobject)NULL;
}

jmid = (*env)->GetMethodID(env, lCls, "getVal", "()D");
jfid = (*env)->GetFieldID(env, lCls, "lVal", "D");
if (jfid == NULL || jmid == NULL){
printf("Problem finding jmid or jfid");
return (jobject)NULL;
}

lMidVal = (*env)->CallDoubleMethod(env, inObject, jmid);
lFidVal = (*env)->GetDoubleField(env, inObject, jfid);

printf("Mid Val: %d Fid Val: %d", lMidVal, lFidVal);
//more stuff done here, irrelevent
return result;
}
Just to clarify, all the JNI works fine, it runs, returns a null object, and
otherwise works fine. I simply cannot get the values out from the object passed
to the native.
Nathan Washor
njwashor@yahoo.com
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nathan,
Welcome to JavaRanch!
Your JNI code looks fine; I can't guarantee it's correct but it looks correct to me. I do see one issue though: the line

uses the "%d" format specifier to print doubles. %d is actually for ints, not doubles; you want, for example, "%lf" to print a double. The results of making this mistake are undefined, so printing zeros is definitely one of the possible outcomes. Hopefully this is the only problem.
 
Nathan Washor
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jess, thanks for the reply. Yup, that was the whole stupid problem. I was printing out the result incorrectly. Oops Thanks for the help though.
 
Who knew that furniture could be so violent? Put this tiny ad out there to see what happens:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic