johnkrishna
johnkrishna

Reputation: 160

how to pass pointers from java to C in JNI?

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.

Upvotes: 3

Views: 8347

Answers (3)

Malcolm
Malcolm

Reputation: 41510

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.

Upvotes: 6

Dave
Dave

Reputation: 4597

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

Upvotes: 0

Mehdi
Mehdi

Reputation: 1531

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

Upvotes: 2

Related Questions