Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6050731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:43:17+00:00 2026-05-23T07:43:17+00:00

I am new to JNI and Android NDK, I want to call a Java

  • 0

I am new to JNI and Android NDK, I want to call a Java API from one of my c language thread. While calling the Java method I am getting error:

Can some one guide me, What is the best approach to call Java method from a c thread?

Below Code is giving me, Invalid JNI Reference Error

JavaVM *m_vm;
jobject obj;

JNIEXPORT  jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
    m_vm = vm;
    return JNI_VERSION_1_4;

}


int aWorkerThread() {
    JNIEnv      *env;
    jmethodID   mid;

    (*m_vm)->GetEnv(m_vm, (void**) &env, JNI_VERSION_1_4);
    (*m_vm)->AttachCurrentThread(m_vm, &env, NULL); 

    jclass cls      = (*env)->GetObjectClass(env, obj)
    jmethodID mid   = (*env)->GetMethodID(env, cls, "callbackfromNative", "(Ljava/lang/String;)V");

    if (mid == NULL) {
           return -1;  // method not found
    }

    const char *pchar   = "Hello From C layer";
    jstring jStr        = NULL;
    jStr = (*env)->NewStringUTF(env, pchar);
    (*env)->CallVoidMethod(env, obj, mid,jStr); 

    return 0;
}

Setting obj into a Global Variable

void Java_com_my_demo_init(JNIEnv * env, jobject this, jstring logThis)  
 {  
     int nErrorCode;
     jboolean isCopy;  
     const char * szLogThis = (*env)->GetStringUTFChars(env, logThis, &isCopy);  
     __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "FromJava: [%s]", szLogThis); 

     obj = this; /*Lets assign obj here*/

     /*Some other c code here..*/

     (*env)->ReleaseStringUTFChars(env, logThis, szLogThis);  
 }

Error Message:

06-20 13:12:03.239: WARN/dalvikvm(753): JNI WARNING: 0x44e83338 is not a valid JNI reference
06-20 13:12:03.239: WARN/dalvikvm(753):              in Ldalvik/system/NativeStart;.run ()V  (GetObjectClass)

...
06-20 13:12:03.279: ERROR/dalvikvm(753): VM aborting
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:43:17+00:00Added an answer on May 23, 2026 at 7:43 am

    You’ve gotten the object class (cls) and the class method (mid) but you don’t actually have an instance of the object. It’s like having a null object and trying to call one of its methods. You need to set obj using either the JNI call AllocObject or NewObject, which like executing the “new” Java operator on the object. AllocObject is different from NewObject in that the constructor for the object isn’t called.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to call a C subroutine from Java. I'm using JNI. I have
I'm new in Android JNI. I'm sending JAVA object from my java file to
I'd like to pass java class object to JNI method, And I want to
I started using JNI to call Java classes from C++ some weeks ago and
I want to replace the current Java process by a new one just like
Hi I am new to android Programming and NDK.Yet i am not clear with
I'm new to JNI, i'm developing a native library for an Android project. I
I'm trying to create a Qt main windows from Java using JNI directly and
I'm moving a project to the new Android Native Development Kit (i.e. JNI) and
I am pretty new to working with JNI on Android, and I've been writing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.