Java C ++ Shell Design with JNI

I am currently writing a JNI wrapper for a C ++ class, and I'm not sure what I have done so far is the most correct way.

As I understand it, it is not possible to declare a Java constructor native, so I wrote something like:

package log;

public class Logger
{
    private long internalPtr = 0;

    private native long createNativeInstance(String application, String module, String function);

    public Logger(String application, String module, String function)
    {
        this.internalPtr = createNativeInstance(application, module, function);
    }

    public native String getApplication();

    static { System.loadLibrary("log_java"); }
}

Basically, the field internalPtrcontains a pointer to my main C ++ instance, and I create it in a pure Java constructor using the method static native createNativeInstance().

Is this the right way to do something?

Another question that I could get an answer to is: "How do I handle release?"

My Java skills are extremely limited, so feel free to offer even more β€œobvious” solutions.

Many thanks.

+5
1

, C/++. , , .

. C/++, - C/++, java- ( finalize). - :

private native void destroyNativeInstance( long p_internalPtr );

public void finalize()
{
    destroyNativeInstance( this.internalPtr );
}
+2

All Articles