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

  • Home
  • SEARCH
  • 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 5988105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:54:44+00:00 2026-05-22T22:54:44+00:00

Here I create a class in JAVA in which I have function (callback) which

  • 0

Here I create a class in JAVA in which I have function (callback) which I must call from C file.

class DSMInitializeClassParameter {

    /**
     * Callback function for DSM Initialize.
     */
    public void DSMInitializeCallback( ) {

        // Write Message To Logs.
        System.out.println( "Dsm Initialize Callback called." );
    }
}

For that I have created native method which must be called.

public class DsmLibraryTest extends Activity {
     // Some code ....

     // Create a DSMInitializeClassParameter  class object.
     DSMInitializeClassParameter object = new DSMInitializeClassParameter();
     // Call native method with given object.
     nativeMethod( object );

     // Some code ....

     // Implementation of native method.
     public native int nativeMethod(DSMInitializeClassParameter classObject);
}

In C file I have following:

dsmResult_t dsmInitializeCall( dsmResult_t status, void * pUserData, dsmEvent_t * hEvent ) {

    (*env)->CallVoidMethod(env, classObject, mid);
}

JNIEXPORT jint JNICALL Java_com_Dsm_Test_DsmLibraryTest_nativeMethod(JNIEnv* env, jobject obj, jobject classObject) {
    // This function loads a locally-defined class. It searches the directories and zip
    // files specified by the CLASSPATH environment variable for the class with the specified name.
    jclass cls = (*env)->FindClass( env, "com/Dsm/Test/DSMInitializeClassParameter" );
    // Get java Method.
    jmethodID mid = (*env)->GetMethodID(env, cls, "DSMInitializeCallback", "()V");
    // If no method was found return -1;
    if( mid == NULL ) {
        return -1;
    }

    // Call DSM Initialize Callback Function and return value.
    return dsmInitialize( dsmInitializeCall, NULL );
}

how you can see I want to call (*env)->CallVoidMethod(env, classObject, mid); from dsmInitializeCall function, but how I can call it if I have no env, classObject and mid I try with statics but it 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-05-22T22:54:45+00:00Added an answer on May 22, 2026 at 10:54 pm

    It’s generally unsafe to cache a JNIEnv* instance and keep using it, as it varies depending on the currently active thread. You can save a JavaVM* instance, which will never change. In a native initializer function, call GetJavaVM and pass it the address of a JavaVM pointer:

    static JavaVM *jvm;
    JNIEXPORT void JNICALL Java_SomeClass_init(JNIEnv *env, jclass) {
        int status = (*env)->GetJavaVM(env, &jvm);
        if(status != 0) {
            // Fail!
        }
    }
    

    Now you can use that JavaVM* to get the current JNIEnv* with AttachCurrentThread:

    dsmResult_t dsmInitializeCall( dsmResult_t status, void * pUserData, dsmEvent_t * hEvent ) {
        JNIEnv *env;
        (*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL);
        (*env)->CallVoidMethod(env, classObject, mid);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a java app. I have one class q2a2 which is a
JAVA Code Here is some part of my code that I have written in
I have an activity called message.java which is bound to the service GPS.java. The
Here's my issue, I'd like to mock a class that creates a thread at
My goal here is to create a very simple template language. At the moment,
Here's my table: CREATE TABLE `alums_alumphoto` ( `id` int(11) NOT NULL auto_increment, `alum_id` int(11)
Here's my situation: I'm trying to create a SSL certificate that will be installed
Here's what I'd like to do: I want to create a library project that
Here is an example of what I've got going on: CREATE TABLE Parent (id
Here is a snippet of the code : HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(request.RawUrl); WebRequest.DefaultWebProxy =

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.