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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:51:05+00:00 2026-06-11T08:51:05+00:00

I am storing off JNIEnv in a global so I can call static java

  • 0

I am storing off JNIEnv in a global so I can call static java methods later. But is it nessasary to store off a global pointer to the JNIEnv, they way one would with any other java object, or is it a special case that does not require this.

JNIEnv* globalEnvPointer;

[JNICALL etc] void init(JNIENv* env, [etc])
{
   //required?
   globalEnvPointer = (JNIENv*) (env*)->GetGlobalRef(env, env);
   //or is this OK?
   globalEnvPointer = env;
}

Edit

I’m bing a bit dumb here, all the methods that will use globalEnvPointer, are invoked within my init because my init is actually my c program’s main method, which won’t return until the end of the program. I am also using no other threads in the c program. I think this simplifies the answer.

JNIEnv* globalEnvPointer;

[JNICALL etc] void main(JNIENv* env, [etc])
{
   //required?
   globalEnvPointer = (JNIENv*) (env*)->GetGlobalRef(env, env);
   //or is this OK?
   globalEnvPointer = env;
   someMethod();
}

void someMethod()
{
   //use globalEnvPointer here
}
  • 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-11T08:51:07+00:00Added an answer on June 11, 2026 at 8:51 am

    You cannot cache the JNIEnv pointer. Read about it here:

    The JNI interface pointer (JNIEnv) is valid only in the current thread. Should another thread need to access the Java VM, it must first call AttachCurrentThread() to attach itself to the VM and obtain a JNI interface pointer. Once attached to the VM, a native thread works just like an ordinary Java thread running inside a native method. The native thread remains attached to the VM until it calls DetachCurrentThread() to detach itself.

    What you can do is to cache the JavaVM pointer instead.

    static JavaVM *jvm;
    
    [JNICALL etc] void init(JNIENv* env, [etc])
    {
       jint rs = (*env)->GetJavaVM(env, &jvm);
       assert (rs == JNI_OK);
    }
    

    And then whenever you need then JNIEnv pointer from a context where it is not given you do this:

    void someCallback() {
        JNIEnv *env;
        jint rs = (*jvm)->AttachCurrentThread(jvm, &env, NULL);
        assert (rs == JNI_OK);
        // Use the env pointer...
    }
    

    But whenever you call a native method from Java the env pointer to use is given:

    JNIEXPORT jint JNICALL Java_package_Class_method(JNIEnv *env, jobject obj) {
        // just use the env pointer as is.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off, I'm not all that familiar with cookies but I know how they
Im thinking this is correct, but probably WAY off. I have a string formatted
Storing the current_user in a string after they submit a comment <%= f.text_field :commenter,
I have two fields in my database that simply store on and off times
Say I offer user to check off languages she speaks and store it in
When does storing doubles make sense? Assuming you read the data off a decimal-notation
First off I have seen Load Java-Byte-Code at Runtime and it was helpful in
I have been storing my passwords in plain-text for development purposes but want to
I am developing an application in which I am storing call log details from
I always assumed that booleans were more efficient than ints at storing an on/off

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.