如何为Java接口创建JNI以便我可以从C++代码中的接口调用函数?如何为Java接口创建JNI?
具体来说,我有一个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
我还试图用virtual void
在JFoo而不是有一个抽象的C++类,但是,这并不工作,因为你“不能声明字段‘foo123’是抽象类型“。
如何为Java接口创建JNI?
要声明JNI方法,Java有一个特殊的'native'关键字 –
有哟你考虑过查阅文件吗?而不是只是猜测? – EJP
我知道'native'关键字,并且正在使用它(只是没有在我的问题中包含完整的代码),谢谢! 你能帮我指点一下正确的文档吗?我已经做了大量的搜索,但是如果JNI代表Java Native Interface,那么专门创建一个Java接口的JNI是很困难的。 :(我成功创建了一个Java类的JNI,但你说得对,我只是猜测一个接口而不是一个类。 – yoloswag