Using an external java class

I’ve written a java class (which interfaces to a C library via JNI) and which I’d like to use in a sketch.

//**** bezier/BezFit.java
package bezier;
public class BezFit {
    static { System.loadLibrary("bezier"); }
    public static native void fitCurve(double[] result, int n, double delta, double[] d);
}

and I can use this from java like this:

//**** Tst.java
import bezier.BezFit;

public class Tst {
    public static void main(String[] args) {
	    double[] data = {0.01, 0.01, 0.3, 1.0, 0.7, 2.0, 1.0, 2.4, 2.0, 3.0, 3.0, 3.6, 3.4, 4.0, 3.8, 5.0, 4.0, 6.0};
	    double[] bez = new double[8];   
	    BezFit.fitCurve(bez, 9, 0.5, data);
	    System.out.println("control pt 1: " + bez[2] + ", " + bez[3]);
	}
}

Now, what is the simplest way of using BezFit in a single sketch? Do I absolutely have to create a library? I don’t use Eclipse. I use vi and javac :slight_smile:

I had a look at https://forum.processing.org/two/discussion/21049/creating-a-library where it says: “Firstly - are you clear about how Java works, and what happens in Processing under the hood?”

No, I’m not a java programmer and no, I don’t know what happens under the hood :-/

1 Like

I don’t know much about it but I am interested to help but I not today as I ran out of time. I have some c++ experience but unfortunately not in Windows but in linux. Do you have the jar files for jni? You should provide this jar during the building via the classpath on the command line. You might need extra libraries.

Check these links:

using c++ library in processing.org - Processing 2.x and 3.x Forum adding external library with JNI interface - Processing 2.x and 3.x Forum https://processing.org/discourse/beta/num_1147602703.html

Kf

1 Like

Thanks for replying.

? It’s written in C, not C++. And I’m on linux too…

I don’t want to use the C library from Processing (and I never want to touch JNI again either…). I want to use the java class.

Processing includes the JNA library. Can’t remember how easy this is to access from a sketch, but it would be a far easier way of interfacing with the native code than raw JNI.

OK, I ended up installing Netbeans.

Well, you can’t go wrong with that choice! :grin: