Can JNI be used to reference C objects in Java code?

I don't see anywhere (or maybe I simply don't see it), but is there a way to return a C / C object using JNI and use it in Java?

For example (very simple):

class simpleClass{
...
private:
int intVar;
public:
int getIntVar();
void setIntVar(int someNum);
...
}

In my java code, what should I do:

...
simpleClass sc = new simpleClass();
sc.setIntVar(9);
System.out.println(sc.getIntVar());
...

I realize that this is a very simple example, but I'm just looking for concepts - I remember that the class in C is very large, and I want to avoid creating a ton wrapper method

If not, I just hope to save a few days of coding

Solution

No, you can't C and Java ABI are completely different - one, C does not define one Really C has so many functions that it can't map to Java, all of which just don't work How will Java handle C templates? Point to primitive? An object that is not a pointer?

What you can do now is to use swig to generate the correct packaging method for you - it can actually work, not more than you planned:)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>