1 year ago
#383369

Wendell Best
JNI GetStaticFieldID() causes segmentation fault
I have a program Agent that launches a thread call BeaconSender that sends a Beacon to another program called Manager using RMI through Manager's BeaconListener interface. When the manager receives the beacon, it launches a thread called Command that sends two objects (send obj1, receive obj1, sendobj2, receive obj2) through RMI to the Agent calling a method named "execute" (the JNI methods are in C code) which depending on the object (identified by a string variable) executes the method associated with the particular object and returns the results.
The problem: I start the programs in order, Manager first then Agent and when Agent sends it's beacon Manager tries to use the "execute" method then Agent fails for a segmentation fault when it tries to run the JNI methods.
The class for my JNI is:
public class CmdRegister extends UnicastRemoteObject implements CmdAgent{
protected CmdRegister() throws RemoteException {
super();
}
public native GetLocalTime C_GetLocalTime(GetLocalTime alpha);
public native GetVersion C_GetVersion(GetVersion beta);
static{System.loadLibrary("time");}
@Override
public Object execute(String CmdID, Object CmdObject) throws RemoteException {
//System.out.println("Entered C_GetLocalTime1");
if(CmdID.equals("GetLocalTime")){
//System.out.println("Entered C_GetLocalTime2");
return C_GetLocalTime((GetLocalTime) CmdObject);
}
else if(CmdID.equals("GetVersion")){
System.out.println("Entered C_GetLocalTime3");
return C_GetVersion((GetVersion) CmdObject);
}
return null;
}
}
The C code is:
JNIEXPORT jobject JNICALL Java_agent_CmdRegister_C_1GetLocalTime
(JNIEnv *env, jobject obj, jobject alpha){
printf("Entered the C code GetLocalTime\n");
jfieldID fid1 = (*env)->GetStaticFieldID(env, alpha, "time","I");
(*env)->SetStaticIntField(env, alpha, fid1, time(0));
jfieldID fid2 = (*env)->GetStaticFieldID(env, alpha, "valid", "C");
(*env)->SetStaticCharField(env, alpha, fid2, '1');
}
JNIEXPORT jobject JNICALL Java_agent_CmdRegister_C_1GetVersion
(JNIEnv *env, jobject obj, jobject beta){
jfieldID fid = (*env)->GetStaticFieldID(env, beta, "version", "I");
(*env)->SetStaticIntField(env, beta, fid, 123);
}
I know I am missing something, but I can't figure out what. This seems to be what the tutorials I found and the API for JNI says should work.
java
java-native-interface
0 Answers
Your Answer