dcsimg
Problem when calling a Java method from C
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Winston_Evans
Posted On:   Thursday, July 19, 2001 07:00 AM

I incountering some problem calling a Java class from C (JNI). I can find a class and call one of its methods. The problem is that the method instantiad another class and ask this to perform a task. When a start my C code I recieve this message: JNI panic: JNI received a null string this is my Java code: /** * Title: Costumer profile * Description: The Class CostumerProfile retrieve the profile * of a Costumer with a given ID * Copyright: Copyright (c) 2001 * Company: * @author Winston Evans * @version 1.0 */ public class CostumerProfile{ public String getCostumerProfile(int clientId){ JDomCostumer   More>>

I incountering some problem calling a Java class from C (JNI). I can find a class and call one of its methods. The problem is that the method instantiad another class and ask this to perform a task. When a start my C code I recieve
this message:

JNI panic: JNI received a null string


this is my Java code:

			

/**
* Title: Costumer profile
* Description: The Class CostumerProfile retrieve the profile
* of a Costumer with a given ID
* Copyright: Copyright (c) 2001
* Company:
* @author Winston Evans
* @version 1.0
*/
public class CostumerProfile{
public String getCostumerProfile(int clientId){
JDomCostumer jcost = new JDomCostumer(clientId);
}

public String test(int val)
{
return val;
}

}




			

This is my C code:

main(int argc, char **argv ) {

jclass cls;
jmethodID mid;

int value=1234567;
jstring returnString;
const jbyte *returnInCString;
//jthrowable jthr;


/* Setup the environment */
JNI_GetDefaultJavaVMInitArgs ( &vm_args );

vm_args.classpath = ".;c:\jdk1.1.8\lib\classes.zip;c:\winston\visualC++\main"; //PATH of the JAVA Classes
JNI_CreateJavaVM(&jvm, &env, &vm_args );

/* Find the class we want to load */
cls = (*env)->FindClass( env, "JDomCostumer" );

mid = (*env)->GetMethodID( env, cls, "getCostumerProfile", "(I)Ljava/lang/String;" );
returnString = (*env)->CallStaticObjectMethod(env, cls, mid, value);
returnInCString = (*env)->GetStringUTFChars(env,returnString,0);

printf("First call to Java returns:"%s".
",returnInCString);


(*env)->ReleaseStringUTFChars(env,returnString,returnInCString);

//printf("First call to Java returns:%d
", (*env)->CallStaticObjectMethod(env, cls, mid, value));

(*jvm)->DestroyJavaVM(jvm);
}

   <<Less

Re: Problem when calling a Java method from C

Posted By:   Alexander_Krapf  
Posted On:   Sunday, July 22, 2001 07:45 AM

Hi Winston,


You have more than one problem in the snippet that you provided.



  1. Your classname is not JDomCustomer but CustomerProfile. You might just have provided the wrong Java snippet though.
  2. You're calling the method as a static method, but it's an instance method.
  3. You're not performing any error checking. Check for exceptions or at least null returns after every call.

You might want to check out JunC++ion, a tool that exposes Java classes as C++ proxy classes and allows you to write your program as follows:



int main()
{
cout << CustomerProfile::getCustomerProfile( 12345 ) << endl;
return 0;
}

Find out more about JunC++ion at http://www.codemesh.com.


Good luck,


Alex

About | Sitemap | Contact