0

I am trying to call Java methods from c++ using JNI. According to my understanding when c++ wants to access non static java methods we need to have instance to java to access those methods.

I am getting runtime error and I am not able to understand where exactly the error is but my code is breaking.I tracked by cout and its breaking while creating the instance

Error is : V [libjvm.so+0x2c3253] methodHandle::methodHandle(Method)+0x33*

And my code is like this: .h file

static jclass clsEventStatus;
static  jmethodID constructorEventStatus;
jobject EventStatusInstance;

.cpp file

jclass EventStatusJNI::clsEventStatus; jmethodID EventStatusJNI::constructorEventStatus;

int EventStatusJNI::initialize() {
  if(clsEventStatus==0) {
    clsEventStatus = StubJava::env->FindClass("metronome/EventStatus");//no error 
    if(clsEventStatus == 0){
      std::cout<<"class Eventstatus Not found";
    } 
    constructorEventStatus = StubJava::env->GetMethodID(clsEventStatus, "<init>", "()V");//no error
   
    return 0;
  }
  return 1;
}

EventStatusJNI::EventStatusJNI(){

EventStatusInstance = StubJava::env->NewObject(clsEventStatus, constructorEventStatus); //its breaking here when call happens
if(EventStatusInstance == 0 ){std::cout<<"No EventStatusInstance found ";}

}

In .java file:

public class EventStatus {
   
    public EventStatus(){
    System.out.println("Entered Event status.java");
    setStatusValue(EventStatusValue.PROCESSING);
    
  }
}

thank you for your time!

Pratvi
  • 11
  • 1
  • Which threads are these calls happening on? `clsEventStatus` holds a local reference, which may have been deleted by the time you try to use it (especially if these calls are taking place on different threads). – Michael Aug 08 '20 at 15:35
  • yes ! I am using POSIX threads and I am creating number of threads .What to do in this case?? – Pratvi Aug 08 '20 at 16:03
  • You could either create a GlobalRef from the LocalRef, which then can be shared across threads. Or you could call `FindClass` whenever you need the class, instead of just once. Note that you also can't share `JNIEnv` pointers across threads. It's not clear from your code whether or not your doing that. – Michael Aug 08 '20 at 16:17
  • Thank you for your answer! Yes JNIEnv is in another file and its common for all methods and files! Do I need to create JNIEnv for all? I have so many bridges(.class file calls) in my project. – Pratvi Aug 08 '20 at 17:15
  • 1
    Each thread should obtain its own `JNIEnv*`. See https://stackoverflow.com/questions/30026030/what-is-the-best-way-to-save-jnienv/30026231#30026231 – Michael Aug 08 '20 at 17:33

0 Answers0