Select Call
Select Call

Reputation: 45

JNI Method not found

I have been receiving this error for my JNI code while I tried find the method ,using GetMethodID, my Java method is in an Interface.

Here is my interface

 public interface printReader 
 {

 public printImg readerPrint(String selectedName) throws Exception;

 }

Native code

WprintImgIMPL.h
class WprintImgIMPL: public IWprintReader  {

public:

    WprintImgIMPL(JNIEnv *env, jobject obj);
   ~WprintImgIMPL(void);

virtual WprintImg  readerPrint(char* readerName) ;
   .....
   .....
 private:
    JNIEnv *m_Env;
    jobject m_jObj;
 }

WprintImgIMPL.cpp
WprintImg  WprintImgIMPL::readerPrint(char* readerName) {
jclass cls = m_Env->GetObjectClass (m_jObj);

jmethodID mid = m_Env->GetMethodID (cls, "readerPrint", "(Ljava/lang/String;)Lcom/site/name/printImg;");
.......
.......
}

Java code

public class printReaderIMPL implements printReader {
static final String DEBUG_TAG = ""; 
android.net.wifi.WifiManager.MulticastLock lock;

Context _context;

public printReaderIMPL (Context context) {
    _context = context;
}

@Override
public printImg readerPrint(String selectedName) throws Exception { 

    Log.e(DEBUG_TAG, "readerPrint");
     }
}

Constructor/destructor

   WprintImgIMPL(JNIEnv *env, jobject obj){
     m_Env = env;
     m_jobj = env->NewGlobalRef(obj);

  }
  ~WprintImgIMPL(void) {
     m_Env->DeleteGlobalRef(m_jobj);
  }

Error: GetMethodID: method not found: Lcom/site/name/NativeCode;.printImg:(Ljava/lang/String;)Lcom/site/name/printImg;

Signature are checked twice , after failure I generated again using Javap tool .

Thank you if you can input /comment and help in fixing this bug.

Upvotes: 0

Views: 1031

Answers (1)

user207421
user207421

Reputation: 310893

It is invalid to save a JNIEnv* across JNI method calls. It's only valid for the duration of the JNI method you are currently in. Out of the blue, e.g. in arbitrary C++ code, you need to call AttachCurrentThread() to get a current valid JNIEnv*.

However you can cache the methodID. There no need to look it up every time. Look it up in your constructor.

Upvotes: 1

Related Questions