![](/img/trans.png)
[英]How to call getStackTrace method from java native interface (jni)
[英]How to make a JNI for a Java Interface?
如何為 Java 接口創建 JNI,以便我可以在 C++ 代碼中從接口調用函數?
具體來說,我有一個Java接口
public interface Foo {
public void Bar(int a);
}
我試圖為它創建一個 JNI
JFoo.h:
class JFoo {
...
public:
void Bar(int a);
...
};
JFoo.c:
...
void JFoo::Bar(int a) {
//Not sure what to put here. If I don't have then I have issues because
"declaration of 'void JFoo::Bar(int)' outside of class is not
definition"
return;
}
...
所以從另一個 C++ 文件我可以做
JFoo foo123;
foo123 = ... //the Java object which implements the Foo interface is what actually passes in 'foo123'
foo123.bar(5); //This ideally executes the Java object's implementation of bar
我還嘗試在 JFoo 中使用virtual void
來代替抽象 C++ 類,但這不起作用,因為您“不能將字段 'foo123' 聲明為抽象類型”。
如何為 Java 接口制作 JNI?
好吧,它不是那樣工作的:(
你不能這樣走。 JNI 允許您通過 JNI 實現中提供的函數調用本機方法和操作 Java 對象。 但這與一對一映射無關。
看看這里:
https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo025
此示例展示了如何從 C++ 調用類的方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.