How to pass pointers from java to C in JNI?

I have my own method int sum(int *,int *). How to pass parameters for this method from java side.

Edit: the example method that I successfully completed is double gsl_stats_mean (doubleArray, int, int); this method is available in GSL, since I created a shared object, and from the java side I sent the required parameters, and I received the double value as the return value.

+3
source share
3 answers

If the method does not change the reference values, you simply pass the parameters as values ​​and get their addresses in your own 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:

native int sum(int firstAdded, int secondAddend);

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

+6

C Java JNI

+2

If you do not need to use JNI, JNA is probably the best option. I found it much easier to use.

0
source

All Articles