how to pass pointers from java to C in JNI?(java - 如何在JNI中将指针从java传递到C?)
问题描述
我有一个本地方法 int sum(int *,int *)
.我如何从 java 端传递这个方法的参数.
i have a native method int sum(int *,int *)
.How do i pass the parameters for this method from java side.
我成功运行的示例方法是双 gsl_stats_mean(doubleArray,int,int);这个方法在 GSL 中可用,因为我已经创建了共享对象,并且从 java 端我已经发送了所需的参数,并且我得到了 double 作为返回值.
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.
推荐答案
如果方法没有改变引用的值,那么你只需将参数作为值传递,并在本机代码中获取它们的地址:
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);
}
而Java中的方法是:
And the method in Java is:
native int sum(int firstAdded, int secondAddend);
显然,除了 sum
函数之外,您在任何地方都不需要指针,因此没有理由在 Java 中使用它们.
Apparently you don't need the pointers anywhere except in the sum
function, so there is no reason to work with them in Java.
这篇关于java - 如何在JNI中将指针从java传递到C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:java - 如何在JNI中将指针从java传递到C?


基础教程推荐
- 从 python 访问 JVM 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 大摇大摆的枚举 2022-01-01
- Java Swing计时器未清除 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01