Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create java native method for constructor

I am writing a program in Java and I would like to create a native interface for a library written in C++. But I am confused with how to write a native method declaration for a constructor.

Say I have this C++ class and constructor:

 template <class _Tp,class _Val>
  class Arbitrator
  {
  public:
    Arbitrator();
  }

How would I write a native method declaration?

This is what I'm doing so far: package hbot.proxy.bwsal.arbitrator;

public class Arbitrator<Tp, Val>
{
    public native Arbitrator Arbitrator();
}

Is this how I would do this?

Thanks

like image 538
Hunter McMillen Avatar asked Sep 30 '11 22:09

Hunter McMillen


1 Answers

In order to call a Java class constructor from C++ JNI code, you need to use JNI constructs here. Assuming you have passed a reference to the JVM with JNIEnv in your C++ function like this:

// Function declaration

void Java_com_hunter_mcmillen_Arbitrator (JNIEnv *env, jobject thiz) {

// Reference to the Java class that has your method

jclass itemClazz = env->FindClass("com/hunter/mcmillen/myjava/myclasses/Arbitrator");

// Reference to the method in your java class

jmethodID constructor = env->GetMethodID(itemClazz, "<init>", "(Ljava/lang/Object;Ljava/lang/Object)V");

}

And now you can actually call the constructor function in your C++ code.

like image 86
IgorGanapolsky Avatar answered Oct 13 '22 18:10

IgorGanapolsky