我正试图用自定义类类型参数从C++调用一个Java函数。问题是Java函数在从C++调用时会得到垃圾值(代码如下所示(。如果我使用任何其他数据类型(String、float、int(的参数,则相同的函数printClassVar
可以正常工作。
这是Java代码
/*Filename: CallbacksExample.java*/
public class CallbacksExample {
static {
System.loadLibrary("myjni");
}
public static void main(String[] args) {
new CallbacksExample().callback();
}
public native void callback();
public static void printClassVar(Person person)
{
System.out.println("Got callback from C++: " + person.age );
}
}
/*Filename: Person.java*/
public class Person {
public int age;
public Person() {
this.age = 20;
}
public int value() {
return age;
}
}
这是JNI代码
/*Filename: CallbacksExample.cpp*/
#include <iostream>
#include <jni.h>
#include "CallbacksExample.h"
using namespace std;
class Person
{
int age;
public:
Person()
{
age = 5;
}
int value()
{
return age;
}
};
JNIEXPORT void JNICALL
Java_CallbacksExample_callback(JNIEnv *env, jobject jthis)
{
jclass thisClass = env->GetObjectClass(jthis);
Person _per;
Person* class_ptr = &_per;
std::cout << class_ptr->value() << std::endl;
jmethodID printClassVar = env->GetStaticMethodID(thisClass, "printClassVar", "(LPerson;)V");
if (NULL == printClassVar)
return;
env->CallVoidMethod(jthis, printClassVar, &class_ptr);
}
上述代码返回
从C++获得回调:1679598160(或任何垃圾签名的int值(
以下是在C++中用类类型参数调用Java函数的正确方法
/*Filename: CallbacksExample.java*/
public class CallbacksExample {
static {
System.loadLibrary("myjni");
}
public static void main(String[] args) {
new CallbacksExample().callback();
}
public native void callback();
public static void printClassVar(Person person)
{
System.out.println("Got callback from C++: " + person.age );
}
}
/*Filename: Person.java*/
public class Person {
public int age;
public Person() {
this.age = 20;
}
public void set(int x)
{
age = x;
}
public int value() {
return age;
}
}
/*Filename: CallbacksExample.cpp*/
#include <iostream>
#include <jni.h>
#include "CallbacksExample.h"
JNIEXPORT void JNICALL
Java_CallbacksExample_callback(JNIEnv *env, jobject jthis)
{
jclass thisClass = env->GetObjectClass(jthis);
jclass personClass = env->FindClass("Person");
jmethodID class_constructor = env->GetMethodID(personClass, "<init>", "()V"); // no parameters
jobject personObj = env->NewObject(personClass, class_constructor);
auto personMethod = env->GetMethodID(personClass, "set", "(I)V");
env->CallVoidMethod(personObj, personMethod, 15);
jmethodID printClassVar = env->GetStaticMethodID(thisClass, "printClassVar", "(LPerson;)V");
if (NULL == printClassVar)
return;
env->CallVoidMethod(jthis, printClassVar, personObj);
}