I am successfully building a shared library that is C++ and loading it in my Java program with System.loadLibrary()
My class in the C++ file was called Classifier
How do I instantiate a new "Classifier" object in java? Do I have to compile and include the java files that are generated from Swig to do such a thing? If I do not want to do that, can I just use the methods from the class? 开发者_如何学运维
Used correctly, SWIG will have generated a wrapper Java class called "Classifier."
Yes, this needs to be compiled -- e.g., by including it in your IDE project and/or your build.
The SWIG documentation for Java shows how to instantiate a C++ object from Java:
C++ classes are wrapped by Java classes as well. For example, if you have this class,
class List {
public:
List();
~List();
void insert(char *item);
...
you can use it in Java like this:
List l = new List();
l.insert("Ale");
...
A few other thoughts:
- You can ask SWIG to place the Java class in a package of your choosing, with the -package option on the SWIG command line.
- I personally keep generated code in a separate source tree. You'll be periodically deleting it, and don't want to accidentally delete non-generated code.
- If you do not need access to any C++ classes in your Java code, you might find JNA easier to work with than SWIG.
精彩评论