JNI trouble

Hi,

Below is my attempt at calling a C++ function (DllLoader.processQuery), which in turn calls a Java function (getParameter) of the calling class. The execution reaches the C++ function, the trouble is that the jmethodID I get is NULL. Can you tell me where I went wrong?

Thanks for your help,

Miguel

// java code

publicclass RouteServletextends HttpServlet{

publicvoid doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException{

String respStr = DllLoader.processQuery();

}

public String getParameter(String name){

return request.getParameter(name);

}

}

class DllLoader{

publicstaticnative String processQuery();

}

// C++ code

JNIEXPORT jstring JNICALL Java_DllLoader_processQuery

(JNIEnv *env, jclass cl){

jmethodID id_getParameter = env->GetMethodID(cl,"getParameter","(Ljava/lang/String;)Ljava/lang/String;");

[1748 byte] By [MCorazao3a] at [2007-11-26 20:17:48]
# 1

Miguel,

in your call:

env->GetMethodID(cl,"getParameter","(Ljava/lang/String

;)Ljava/lang/String;");

cl referes to class DllLoader and not RouteServlet. getParameter is not a method of DllLoader but of RouteServlet.

to get the right class you need to call:

jclass right_cl= env->FindClass("RouteServlet ");

Daniel

Message was edited by:

Daniel.Barkan

Message was edited by:

Daniel.Barkan

Daniel.Barkana at 2007-7-10 0:41:11 > top of Java-index,Java Essentials,Java Programming...
# 2

Daniel,

Thank you so much for your help. It definitely put me on the right track. However I'm not at the end of the trip yet. You will notice below that I try to call the Java method using the objct id I got with your help. The trouble is that I get a null value for jvalueObj. If you are kind enough to let me know what is wrong this time, perhaps you can take a look at the rest of the code where I attempt to cast the object I will hopefully get to a jstring, and then get the character string.

Thanks again,

Miguel

public class RouteServlet extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

String respStr = DllLoader.processQuery(this);

}

public String getParameter(String name) {

return request.getParameter(name);

}

}

class DllLoader {

public static native String processQuery(RouteServlet obj);

}

JNIEXPORT jstring JNICALL Java_DllLoader_processQuery

(JNIEnv *env, jclass cl, jobject rtServ) {

jclass rtServ_cl= env->FindClass("RouteServlet");

jmethodID id_getParameter = env->GetMethodID(rtServ_cl,"getParameter","(Ljava/lang/String;)Ljava/lang/String;");

jstring jstrParam = env->NewStringUTF("cmd");

jobject jvalueObj = env->CallObjectMethod(rtServ,id_getParameter,jstrParam);

jstring jvalueStr = (jstring)jvalueObj;

jsize lengthStr = env->GetStringUTFLength(jvalueStr);

const jbyte *bytesStr = (const signed char *)env->GetStringUTFChars(jvalueStr,JNI_FALSE);

MCorazao3a at 2007-7-10 0:41:11 > top of Java-index,Java Essentials,Java Programming...
# 3

> Daniel,

> Thank you so much for your help. It definitely put me

> on the right track. However I'm not at the end of the

> trip yet. You will notice below that I try to call

> the Java method using the objct id I got with your

> help. The trouble is that I get a null value for

> jvalueObj.

You should be checking the return values of all your JNI calls looking for errors/exceptions.

Niceguy1a at 2007-7-10 0:41:11 > top of Java-index,Java Essentials,Java Programming...
# 4

Miguel,

Are you getting to the java code? You can find out by debugging or adding some printing in the java code of the method you call. If you are getting to the java code then it is a java problem and has nothing to do with JNI - good news. If you are not getting to the java code, you are probably getting the wrong method.

Daniel

Daniel.Barkana at 2007-7-10 0:41:12 > top of Java-index,Java Essentials,Java Programming...