如何通过引用将 List 从 Java 作为向量传递给 JNI C++?

我刚接触 C++ 和 JNI


流程应该是这样的


我从 Java 向 JNI 传递一个空List值,JNIloadData(std::vector<int>)从中调用方法MyClass,此方法用数据填充我的列表。


所以,问题是


我有


class MyClass {

public:

MyClass();

~MyClass();


void loadData(std::vector<int> & vector);

};



void MyClass::loadData(std::vector<int> & vector)

{

const int size = 10;


vector.resize(size);


for (int i = 0; i < size; ++i) {

    vector.push_back(4);

}

}

这是我用纯 C++ 编写的方法,现在我需要像这样从 Java 使用它


public native void fillListWithData(List<Integer> list);

所以,我在 JNI 中写了这样的方法来关联它们


extern "C" JNIEXPORT void JNICALL

Java_com_google_ar_core_examples_java

_helloar_HelloArActivity_fillListWithData(

    JNIEnv *env,

    jobject /* this */,

    jobject input

) {

myClass->loadData("HERE I NEED TO PASS MY " input);

}

在这里我应该如何调用这个方法


public void TEST(){

    List<Integer> list = new ArrayList<>();

    fillListWithData(list);

    Log.e("TAG", "HERE I NEED TO HAVE A LIST WITH FILLED DATA");

}

我不明白如何通过 JNI 将此列表通过引用传递给 C++...


任何想法赞赏


皈依舞
浏览 269回答 2
2回答

幕布斯6054654

在这种情况下,它非常简单。您所要做的就是传递给您的本机代码并使用基于访问的方法List将其填充到内部JNIJNI#include <vector>#include "jni.h"#include "recipeNo046_FillTheList.h"using namespace std;JNIEXPORT void JNICALL Java_recipeNo046_FillTheList_fillTheList&nbsp; (JNIEnv *env, jclass cls, jobject obj) {&nbsp; vector<int> vect { 1, 2, 3 };&nbsp; jclass listClass = env->FindClass("java/util/List");&nbsp; if(listClass == NULL) {&nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // alternatively, throw exception (recipeNo019)&nbsp; }&nbsp; jclass integerClass = env->FindClass("java/lang/Integer");&nbsp; if(integerClass == NULL) {&nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // alternatively, throw exception (recipeNo019)&nbsp; }&nbsp; jmethodID addMethodID = env->GetMethodID(listClass, "add", "(Ljava/lang/Object;)Z");&nbsp; if(addMethodID == NULL) {&nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;- || -&nbsp; }&nbsp; jmethodID integerConstructorID = env->GetMethodID(integerClass, "<init>", "(I)V");&nbsp; if(integerConstructorID == NULL) {&nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;- || -&nbsp; }&nbsp; for(int i : vect) {&nbsp; &nbsp; // Now, we have object created by Integer(i)&nbsp; &nbsp; jobject integerValue = env->NewObject(integerClass, integerConstructorID, i);&nbsp; &nbsp; if(integerValue == NULL) {&nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; env->CallBooleanMethod(obj, addMethodID, integerValue);&nbsp; }&nbsp; env->DeleteLocalRef(listClass);&nbsp; env->DeleteLocalRef(integerClass);}请注意,您不必List在内部创建对象,JNI因为您已经在内部C++代码中创建了它。它作为native方法的参数传递。您可以在此处找到完整的示例代码:https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo046运行代码后,您可以看到C++通过List对象传递的数据。> make test/Library/Java/JavaVirtualMachines/jdk-12.0.1.jdk/Contents/Home/bin/java -Djava.library.path=:./lib -cp target recipeNo046.FillTheListlibrary: :./lib123

繁花如伊

提到的答案似乎适用于 Windows 10,但不适用于 Windows 8。Windows8 不支持 Cpp 库中的任何容器。报告的错误将是 - “%1 不是有效的 Win32 应用程序”,这在运行 Java 程序时发生。此处生成的 dll 文件似乎是问题所在。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java