Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return object from java native method

I would like to call a method through a native java interface, which returns an object.

This is my native method

public native Node getProjectionPoint(double lat, double lon);  

Node class

 public class Node {        
    private String id;
    private double latitude;
    private double longitude;
}

C header file

JNIEXPORT jobject JNICALL Java_org_smartcar_serverdatainterface_shared_services_CppConnector_getProjectionPoint (JNIEnv *env, jobject obj, jdouble lat, jdouble lon);

How could I create an object and return it to java?

like image 743
JanithOCoder Avatar asked Mar 10 '14 13:03

JanithOCoder


1 Answers

I sort out the problem

JNIEXPORT jobject JNICALL Java_org_smartcar_serverdatainterface_shared_services_CppConnector_getProjectionPoint
  (JNIEnv *env, jobject obj, jdouble lat, jdouble lon)
{
    jclass class = (*env)->FindClass(env,"org/smartcar/serverdatainterface/shared/businessentities/Node");

    if (NULL == class)
        PrintError ("class");

    jmethodID cid = (*env)->GetMethodID(env,class, "<init>", "(DD)V");

   if (NULL == cid)
       PrintError ("method");

   return (*env)->NewObject(env, class, cid, lat, lon);
}

this works perfectly

like image 87
JanithOCoder Avatar answered Oct 17 '22 09:10

JanithOCoder