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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:59:06+00:00 2026-06-13T01:59:06+00:00

I have learned that the JNI interface pointer (JNIEnv *) is only valid in

  • 0

I have learned that the JNI interface pointer (JNIEnv *) is only valid in the current thread. Suppose I started a new thread inside a native method; how it can asynchronously send events to a Java method? As this new thread can’t have a reference of (JNIEnv *). Storing a global variable for (JNIEnv *) apparently will not work?

  • 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-13T01:59:07+00:00Added an answer on June 13, 2026 at 1:59 am

    Within synchronous calls using JNI from Java to C++ the "environment" has already been setup by the JVM, however going in the other direction from an arbitrary C++ thread it may not have been

    Therefore you need to follow these steps

    • get hold of the JVM environment context using GetEnv
    • attach the context if necessary using AttachCurrentThread
    • call the method as normal using CallVoidMethod
    • detach using DetachCurrentThread

    Full example. Note I have written about this in the past in more detail on my blog

    JavaVM* g_vm;
    env->GetJavaVM(&g_vm);
    
    void callback(int val) {
        JNIEnv * g_env;
        // double check it's all ok
        int getEnvStat = g_vm->GetEnv((void **)&g_env, JNI_VERSION_1_6);
        if (getEnvStat == JNI_EDETACHED) {
            std::cout << "GetEnv: not attached" << std::endl;
            if (g_vm->AttachCurrentThread((void **) &g_env, NULL) != 0) {
                std::cout << "Failed to attach" << std::endl;
            }
        } else if (getEnvStat == JNI_OK) {
            //
        } else if (getEnvStat == JNI_EVERSION) {
            std::cout << "GetEnv: version not supported" << std::endl;
        }
    
        g_env->CallVoidMethod(g_obj, g_mid, val);
    
        if (g_env->ExceptionCheck()) {
            g_env->ExceptionDescribe();
        }
    
        g_vm->DetachCurrentThread();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to Box2d, and I have just learned that it does not
I just learned (the hard way) that Java Component s can only have one
I have learned that browsers can only load a few files from the same
I have learned that a pointer points to a memory address so i can
I am new to sql (and sqlite) and so far I have learned that
I have learned that Windows uses UTF-16LE on x86/x64 systems. What about Linux? Which
In my studies, I have learned that if you want to prevent users from
I'm required to write a JCE provider. I have learned that I need to
I have learned from various tutorial that If a client can reasonably be expected
I recently learned that all stl containers have swap function: i.e. c1.swap(c2); will lead

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.