3

i have a native method int sum(int *,int *).How do i pass the parameters for this method from java side.

Edit:the example method which i ran successfully is double gsl_stats_mean(doubleArray,int,int); this method is available in GSL ,for that i have created shared object and from java side i had sent required parameters and i got the double as a return value.

johnkrishna
  • 160
  • 2
  • 11

3 Answers3

6

If the method doesn't change the referenced values, then you just pass parameters as values, and get their addresses in native code:

JNIEXPORT jint JNICALL Java_com_example_Summator_sum(JNIEnv *env, jobject thisObj,
        jint firstAddend, jint secondAddend) {
     return (jint) sum(&firstAddend, &secondAddend);
}

And the method in Java is:

native int sum(int firstAdded, int secondAddend);

Apparently you don't need the pointers anywhere except in the sum function, so there is no reason to work with them in Java.

Malcolm
  • 41,014
  • 11
  • 68
  • 91
  • ya you are absolutely correct.but the problem is the definition of the method is not with me,i just have a permission to make use of that.so i need a methodology to pass a pointer from java side. – johnkrishna Apr 30 '12 at 17:30
  • @johnkrishna But you said you use JNI, this means that you define methods in Java as native and implement them in native code. If you're not allowed to do this, how are you planning to use JNI anyway? – Malcolm Apr 30 '12 at 20:10
  • all the stuff being discussed is correct.but my main intention of this post is what to be passed from java side when a native method is expecting the pointer,and how. – johnkrishna May 01 '12 at 17:03
  • @johnkrishna When a native function expects a pointer, you pass something that will be converted into a pointer on the native side. My answer shows how it is done. If you want to pass a pointer from Java to the native code, you can do this easily via a long parameter. But its value still has to originate somewhere in the native code, then it may be passed to Java and back. If you want to get a pointer to a field or variable stored in Java, this is obviously not possible because Java isolates the program from direct memory management. – Malcolm May 01 '12 at 17:49
  • ok,now i straightaway will come to the point.we have an open source library GSL,where we wil not have the methods definitions with us,but still we can create shared objects for them and we can use those functions via jni(calling those methods from java side).all is well when the native method expects primitive datatypes and i have successfully used them(note that i did not have the function definition with me).the hurdle is there are methods which are expecting pointers as their parameters.how do i pass the parameters to make use of their functionality.Thanks for your cooperation. – johnkrishna May 02 '12 at 06:32
  • @johnkrishna How exactly could you use them? JNI expects specific method signatures, which this library simply doesn't have. You talk as if you use JNA, not JNI. In JNA what you seek is achieved by creating an `IntByReference`. If you use JNI, maybe you could add to the question an example of using a function without pointers with both C and Java code? – Malcolm May 02 '12 at 12:59
  • i have edited my question with a sample working method.sorry for the clumsy editing(i dont know much about how to post code blocks here). – johnkrishna May 03 '12 at 06:21
  • @johnkrishna So far I see that you don't write anything in C yourself. But could you please show the code in Java which calls it? – Malcolm May 03 '12 at 16:07
2

Take a look in this discussion it's about Passing pointers between C and Java through JNI

Community
  • 1
  • 1
Mehdi
  • 1,494
  • 5
  • 33
  • 53
  • Thanks for your response,but the discussion includes more of structures than the pointers.can you please brief me how to pass a pointer from java to c alone. – johnkrishna Apr 30 '12 at 11:18
0

Unless you're required to use JNI, JNA is probably the better option. I found it much easier to use.

Dave
  • 4,546
  • 2
  • 38
  • 59