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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:02:02+00:00 2026-06-15T15:02:02+00:00

I want to call Java class methods from a cpp file that receives call

  • 0

I want to call Java class methods from a cpp file that receives call backs from another executable.

To achieve this, I have retrieved a JavaVM pointer using the android::AndroidRuntime::getJavaVM() method in the .cpp file that directly receives JNI method calls. I am sharing this JavaVM pointer via the constructor to the eventual .cpp file where I call required Java methods as follows:

/* All the required objects(JNIEnv*,jclass,jmethodID,etc) are appropriately declared. */
**JNIEnv* env;
jvm->AttachCurrentThread(&env, NULL);
clazz = env->FindClass("com/skype/ref/NativeCodeCaller");
readFromAudioRecord = env->GetStaticMethodID(clazz, "readFromAudioRecord", "([B)I");
writeToAudioTrack = env->GetStaticMethodID(clazz, "writeToAudioTrack", "([B)I");** 

However, I get a SIGSEGV fault running this code.

According to the JNI documentation this seems to be the appropriate way to obtain JNIEnv in arbitary contexts: http://java.sun.com/docs/books/jni/html/other.html#26206

Any help in this regard will be appreciated.

Regards,
Neeraj

  • 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-06-15T15:02:03+00:00Added an answer on June 15, 2026 at 3:02 pm

    Global references will NOT prevent a segmentation fault in a new thread if you try to use a JNIEnv or JavaVM reference without attaching the thread to the VM. You were doing it properly the first time around, Mārtiņš Možeiko is mistaken in implying that there was something wrong with what you were doing.

    Don’t remove it, just learn how to use it. That guy doesn’t know what he’s talking about, if it’s in jni.h you can be pretty sure it’s not going anywhere. The reason it’s not documented is probably because it’s ridiculously self explanatory. You don’t need to create GlobalReference objects or anything either, just do something like this:

    #include <jni.h>
    #include <string.h>
    #include <stdio.h>
    #include <android/log.h>
    #include <linux/threads.h>
    #include <pthread.h>
    
    #define  LOG_TAG    "[NDK]"
    #define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
    #define  LOGW(...)  __android_log_print(ANDROID_LOG_WARN,LOG_TAG,__VA_ARGS__)
    #define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
    
    static pthread_mutex_t thread_mutex;
    static pthread_t thread;
    static JNIEnv* jniENV;
    
    void *threadLoop()
    {
        int exiting;
        JavaVM* jvm;
        int gotVM = (*jniENV)->GetJavaVM(jniENV,&jvm);
        LOGI("Got JVM: %s", (gotVM ? "false" : "true") );
        jclass javaClass;
        jmethodID javaMethodId;
        int attached = (*jvm)->AttachCurrentThread(jvm, &jniENV,NULL);
        if(attached>0)
        {
            LOGE("Failed to attach thread to JavaVM");
            exiting = 1;
        }
        else{
            javaClass= (*jniENV)->FindClass(jniENV, "com/justinbuser/nativecore/NativeThread");
            javaMethodId= (*jniENV)->GetStaticMethodID(jniENV, javaClass, "javaMethodName", "()V");
        }
        while(!exiting)
        {
            pthread_mutex_lock(&thread_mutex);
            (*jniENV)->CallStaticVoidMethod(jniENV, javaClass, javaMethodId);
            pthread_mutex_unlock(&thread_mutex);
        }
        LOGE("Thread Loop Exiting");
        void* retval;
        pthread_exit(retval);
        return retval;
    }
    
    void start_thread(){
        if(thread < 1)
            {
                if(pthread_mutex_init(&thread_mutex, NULL) != 0)
                {
                    LOGE( "Error initing mutex" );
                }
                if(pthread_create(&thread, NULL, threadLoop, NULL) == 0)
                {
                    LOGI( "Started thread#: %d", thread);
                    if(pthread_detach(thread)!=0)
                    {
                        LOGE( "Error detaching thread" );
                    }
                }
                else
                {
                    LOGE( "Error starting thread" );
                }
            }
    }
    
    JNIEXPORT void JNICALL
    Java_com_justinbuser_nativecore_NativeMethods_startThread(JNIEnv * env, jobject this){
        jniENV = env;
        start_thread();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my application I want to call service from Receiver. This is my NotificationReceiver.java
I want to call a few static methods of a CPP class defined in
I want to call a C subroutine from Java. I'm using JNI. I have
I have Class in java called XMLDOMDocument this class have some methods CreateXML ,
I want to call shell script on windows environment using java code. I am
I want to call a method say success(message) of javascript from android activity. I
I want to call a web service that requires an authentication cookie. I have
How do I self-instantiate a Java class? I should not use any file other
I've generated .class files from a web service. Now I want to use them
I started using JNI to call Java classes from C++ some weeks ago 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.