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

The Archive Base Latest Questions

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

I read on on IBM that To access Java objects’ fields and invoke their

  • 0

I read on on IBM that

To access Java objects’ fields and invoke their methods, native code
must make calls to FindClass(), GetFieldID(), GetMethodId(), and
GetStaticMethodID(). In the case of GetFieldID(), GetMethodID(), and
GetStaticMethodID(), the IDs returned for a given class don’t change
for the lifetime of the JVM process. But the call to get the field or
method can require significant work in the JVM, because fields and
methods might have been inherited from superclasses, making the JVM
walk up the class hierarchy to find them. Because the IDs are the same
for a given class, you should look them up once and then reuse them.
Similarly, looking up class objects can be expensive, so they should
be cached as well.

How does one cache the methodID, fieldID, and class objects in JNI?
Are there built-in methods or a specific procedure that has to be followed?

  • 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-16T01:53:15+00:00Added an answer on June 16, 2026 at 1:53 am

    There is no built-in methodology to follow, however here is a somewhat standard, clean, and repeatable implementation showing how I practice IBM’s recommendation.

    I am going to assume you are calling your DLL from Java and you are referencing it multiple times throughout your application life-cycle.

    The sample Native Java Class is named org.stackoverflow.jni.NativeClazz, which will implement the 2 built-in JNI methods JNI_OnLoad() and JNI_OnUnload().

    void JNI_OnLoad(JavaVM *vm, void *reserved): This method will be used to register the Class IDs as global variables and assign the Method IDs and Field IDs to static variables. The method is automatically called when the driver is loaded by the Java VM; it is only called once during the driver life-cycle.

    void JNI_OnUnload(JavaVM *vm, void *reserved): This method will be used to free any global variables registered by JNI_OnLoad(). The VM will automatically call JNI_OnUnload() immediately prior to application shutdown.

    Rationale: It’s my understanding the Class IDs must be registered as global references to maintain the viability of any associated Method ID / Field IDs. If this isn’t done and the class is unloaded from the JVM, on class reload, the Method IDs / Field IDs may be different. If the Class ID is registered as a global reference, the associated Method IDs and Field IDs do not need to be registered as global references. Registering a Class ID as a global reference prevents the associated Java class from unloading, therefore stabilizing the Method ID / Field ID values. Global references, including the Class IDs should be removed in JNI_OnUnload().

    Method IDs and Field IDs are not managed by the native code; they are managed by the virtual machine and are valid until the associated class is unloaded. Field IDs and Method IDs cannot be explicitly deleted before the virtual machine unloads the defining class; they can be left for the VM to handle after unload.

    Sample Code

    Comments in the following C++ code sections explain registering variables globally.

    Here is the Java class BeanObject representing a data object:

    package org.stackoverflow.data;
    
    public class BeanObject {
    
        String foo = "";
        
        public String getFoo() {
         
            return foo;
        }
    }
    

    Here is a skeleton Java class NativeClazz:

    package org.stackoverflow.jni;
    
    import org.stackoverflow.data.BeanObject;
    
    public class NativeClazz {
    
        // Static area for forced initialization
        static {
    
            // Load Native Library (C++); calls JNI_OnLoad()
            System.loadLibrary("Native_Library_File_Name");
        }       
    
        /**
         * A static native method you plan to call.
         */
        public static native void staticNativeMethod(BeanObject bean);
    
        /**
         * A non-static native method you plan to call, to show this also works with 
         * Java class instances.
         */
        public native void instanceNativeMethod(BeanObject bean);
    }
    

    Here is the C++ header file "org_stackoverflow_jni_NativeClazz.h" generated using javah on NativeClazz:

    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
    /* Header for class org_stackoverflow_jni_NativeClazz */
    
    #ifndef _Included_org_stackoverflow_jni_NativeClazz
    #define _Included_org_stackoverflow_jni_NativeClazz
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    /*
     * Class:     org_stackoverflow_jni_NativeClazz_staticNativeMethod
     * Method:    staticNativeMethod
     * Signature: ()V
     */
    JNIEXPORT void JNICALL Java_org_stackoverflow_jni_NativeClazz_staticNativeMethod
      (JNIEnv *, jclass, jobject);
    
    /*
     * Class:     org_stackoverflow_jni_NativeClazz_instanceNativeMethod
     * Method:    instanceNativeMethod
     * Signature: ()V
     */
    JNIEXPORT void JNICALL Java_org_stackoverflow_jni_NativeClazz_instanceNativeMethod
      (JNIEnv *, jobject, jobject);
    
    #ifdef __cplusplus
    }
    #endif
    #endif
    

    Here is the C++ .cpp file implementing the header file:

    #include "org_stackoverflow_jni_NativeClazz.h"
    
    using namespace std;
    
    /**************************************************************
     * Static Global Variables to cache Java Class and Method IDs
     **************************************************************/
    static jclass JC_BeanObject;              // declare for each class
    static jmethodID JMID_BeanObject_getFoo;  // declare for each class method
    
    /**************************************************************
     * Declare general JNI_VERSION for use in JNI_Onload/JNI_OnUnLoad
     * Change value if a Java upgrade requires it (prior: JNI_VERSION_1_6)
     * JNI_VERSION_#_# obtained from jni.h inclusion 
     **************************************************************/
    static jint JNI_VERSION = JNI_VERSION_1_8;
    
    /**************************************************************
     * Initialize the static Class and Method Id variables
     **************************************************************/
    jint JNI_OnLoad(JavaVM* vm, void* reserved) {
    
        // Obtain the JNIEnv from the VM and confirm JNI_VERSION
        JNIEnv* env;
        if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION) != JNI_OK) {
    
            return JNI_ERR;
        }
    
        // Temporary local reference holder
        jclass tempLocalClassRef;
    
        // STEP 1/3 : Load the class id
        tempLocalClassRef = env->FindClass("org/stackoverflow/data/BeanObject");
    
        // STEP 2/3 : Assign the ClassId as a Global Reference
        JC_BeanObject = (jclass) env->NewGlobalRef(tempLocalClassRef);
    
        // STEP 3/3 : Delete the no longer needed local reference
        env->DeleteLocalRef(tempLocalClassRef);
        
        // Load the method id
        JMID_BeanObject_getFoo = env->GetMethodID(JC_BeanObject, "getFoo", "(Ljava/lang/String;)V");
    
        // ... repeat prior line for any other methods of BeanObject
    
        // ... repeat STEPS 1-3 for any other classes; re-use tempLocalClassRef.
    
        // Return the JNI Version as required by method
        return JNI_VERSION;
    }
    
    /**************************************************************
     * Destroy the global static Class Id variables
     **************************************************************/
    void JNI_OnUnload(JavaVM *vm, void *reserved) {
    
        // Obtain the JNIEnv from the VM
        // NOTE: some re-do the JNI Version check here, but I find that redundant
        JNIEnv* env;
        vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION);
    
        // Destroy the global references
        env->DeleteGlobalRef(JC_BeanObject);
        
        // ... repeat for any other global references
    }
    
    /**************************************************************
     * A Static Native Method
     **************************************************************/
    JNIEXPORT void JNICALL Java_org_stackoverflow_jni_NativeClazz_staticNativeMethod
                   (JNIEnv * env, jclass clazz, jobject jBeanObject) {
        
        // Retrieve jstring from the Java Object
        jstring jFoo = (jstring)env->CallObjectMethod(jBeanObject, JMID_BeanObject_getFoo);
    
        // Make accessible to C++
        const char * cFoo = env->GetStringUTFChars(jFoo, NULL);             
    
        // Do something with cFoo...
    
        // Release Resources
        env->ReleaseStringUTFChars(jFoo, cFoo);
        env->DeleteLocalRef(jFoo);
    }
    
    /**************************************************************
     * Instance / Non-Static Native Method
     **************************************************************/
    JNIEXPORT void JNICALL Java_org_stackoverflow_jni_NativeClazz_instanceNativeMethod
                   (JNIEnv * env, jobject selfReference, jobject jBeanObject) {
    
        // Retrieve jstring from the Java Object
        jstring jFoo = (jstring)env->CallObjectMethod(jBeanObject, JMID_BeanObject_getFoo);               
    
        // Make accessible to C++
        const char * cFoo = env->GetStringUTFChars(jFoo, NULL);             
    
        // Do something with cFoo...
    
        // Release Resources
        env->ReleaseStringUTFChars(jFoo, cFoo);
        env->DeleteLocalRef(jFoo);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read http://www.ibm.com/developerworks/java/library/j-jtp05236/index.html I decide to make my lock uncancelable task by try
I've got an ArrayList of objects that I read from a txt file, RegistrationFormBean();
Does Java have a default System Environment Variable that will always be read/appended when
I have read on IBM's Rational ClearCase manual that there is a ct mount
While reading from a site a read that you can not make a global
I have read that select and multi-threaded programming were low performing IO models, for
I want to read the full stack trace of an exception that I capture.
I am using IBM Websphere Application Server v6 and Java 1.4 and am trying
I am trying to compile ioauek3 on our IBM PowerPC machine that is running
We're looking into developing an application that can read from and write to Lotus

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.