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 7537757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:56:16+00:00 2026-05-30T06:56:16+00:00

I’m stuck with this, I need to call a Java Function from c/c++. In

  • 0

I’m stuck with this, I need to call a Java Function from c/c++.

In the examples and tutorials i only see a java app calling a c method, and in this same method calling another java method, but what I want to do is to call a java method from any part of the code. This is what I have:

static JNIEnv mEnv;
static jclass mClassAndroidActivity;
static mMethodSayHello;
JNIEXPORT void JNICALL JNI_FUNCTION(AndroidActivity_nativeInit)(JNIEnv* env, jobject obj, int width, int height)
{
    mEnv = env;
    jclass cls = (*env)->GetObjectClass(env, obj);
    mClassAndroidActivity = (*env)->NewGlobalRef(env, cls);
    mMethodSayHello = (*env)->GetMethodID (env, mClassAndroidActivity, "SayHello", "(Ljava/lang/String;)V");
}

//this method is called from a cpp
void nativeSayHello(char* msg)
{
    jstring string = (*mEnv)->NewStringUTF(mEnv, msg);
    (*mEnv)->CallVoidMethod(mEnv, mClassAndroidActivity, mMethodSayHello, string);
}

but is always crashing, I’ve tried without the NewGlobalRef, using mEnv instead of env in the JNI_Function, I’ve tried getting the method id from the JNI_OnLoad, but always crashes.

This is the log i get:

02-15 18:09:48.520: W/dalvikvm(27904): JNI WARNING: threadid=1 using env from threadid=0

  • 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-30T06:56:17+00:00Added an answer on May 30, 2026 at 6:56 am

    You can’t reuse JNIEnv because it is specific to the calling thread. To call (non-static) Java method from the native code, you need something like this:

    static JavaVM *gJavaVM;
    static jobject gCallbackObject = NULL;
    
    JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) {
        gJavaVM = vm;
        return JNI_VERSION_1_6;
    }
    
    JNIEXPORT void JNICALL JNI_FUNCTION(AndroidActivity_nativeInit)(JNIEnv* env, jobject obj, int width, int height) {
        // ...
        gCallbackObject = (*env)->NewGlobalRef(env, obj);
    }
    
    JNIEXPORT void JNICALL JNI_FUNCTION(AndroidActivity_nativeRelease)(JNIEnv* env, jobject obj) {
        (*env)->DeleteGlobalRef(env, gCallbackObject);
        gCallbackObject = NULL;
    }
    
    //this method is called from native code
    void nativeSayHello(char* msg) {
        int status;
        JNIEnv *env;
        int isAttached = 0;
    
        if (!gCallbackObject) return;
    
        if ((status = (*gJavaVM)->GetEnv(gJavaVM, (void**)&env, JNI_VERSION_1_6)) < 0) {
            if ((status = (*gJavaVM)->AttachCurrentThread(gJavaVM, &env, NULL)) < 0) {
                return;
            }
            isAttached = 1;
        }
    
        jclass cls = (*env)->GetObjectClass(env, gCallbackObject);
        if (!cls) {
            if (isAttached) (*gJavaVM)->DetachCurrentThread(gJavaVM);
            return;
        }
    
        jmethodID method = (*env)->GetMethodID(env, cls, "SayHello", "(Ljava/lang/String;)V");
        if (!method) {
            if (isAttached) (*gJavaVM)->DetachCurrentThread(gJavaVM);
            return;
        }
    
        jstring string = (*mEnv)->NewStringUTF(mEnv, msg);
        (*env)->CallVoidMethod(env, gCallbackObject, method, string);
    
        if (isAttached) (*gJavaVM)->DetachCurrentThread(gJavaVM);
    }
    

    This code snippet is not tested. To prevent memory leak, don’t forget to call nativeRelease() method in your Java code when the reference to the object will not needed any more.

    See The Java Native Interface documentation for more details.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.