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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:19:08+00:00 2026-06-05T11:19:08+00:00

Description In my C++ application class JNIXMLDocument which made some JAVA method calls. In

  • 0

Description

In my C++ application class JNIXMLDocument which made some JAVA method calls. In the constructor of JNIXMLDocument class I attach current thread and set it to my class member JNIEnv* m_JavaEnv and then use it in all methods. Also in the constructor I am trying to find my JAVA class com/fido/android/framework/service/XMLDOMDocument and set it to class member m_XMLDocumentClass and also get that class object from the class and set it to class member m_XMLDocumentObject.

C++ Code

class JNIXMLDocument
{
    /* Constructor **/
    JNIXMLDocument()
    {
        /* Get JNI right version and set it. **/
        jint interface_id = JNI_VERSION_1_2;
        #ifdef JNI_VERSION_1_2
            interface_id = JNI_VERSION_1_2;
        #else
            interface_id = JNI_VERSION_1_1;
        #endif

        /* Trying to attach current thread. **/
        int res = g_JavaVirtualMachine->GetEnv(&m_JavaEnv, interface_id);
        if (res == JNI_EDETACHED || res == JNI_EVERSION) {
            res = g_JavaVirtualMachine->AttachCurrentThread(&m_JavaEnv, NULL);
        }

        /* Get Class from Java **/
        m_XMLDocumentClass = m_JavaEnv->FindClass("com/fido/android/framework/service/XMLDOMDocument");
        if (m_XMLDocumentClass != NULL) {
            /* Call java class constructor. **/
            jmethodID constructor = m_JavaEnv->GetMethodID(m_XMLDocumentClass , "<init>", "()V");
            m_XMLDocumentObject = m_JavaEnv->NewObject(m_XMLDocumentClass , constructor);    

        }

    }

    bool Initialize()
    {
        jmethodID method = m_JavaEnv->GetMethodID(m_XMLDocumentClass, "Initialize", "()Lorg/w3c/dom/Document;");
        jobject document = m_JavaEnv->CallObjectMethod(m_XMLDocumentObject , method);

    }

    private:
        JNIEnv* m_JavaEnv;
        jclass  m_XMLDocumentClass;
        jobject m_XMLDocumentObject;


};

C++ Code (Right way)

class JNIXMLDocument
{
    /* Constructor **/
    JNIXMLDocument()
    {
        /* Get JNI right version and set it. **/
        jint interface_id = JNI_VERSION_1_2;
        #ifdef JNI_VERSION_1_2
            interface_id = JNI_VERSION_1_2;
        #else
            interface_id = JNI_VERSION_1_1;
        #endif

        JNIEnv* env;
        /* Trying to attach current thread. **/
        int res = g_JavaVirtualMachine->GetEnv(&env, interface_id);
        if (res == JNI_EDETACHED || res == JNI_EVERSION) {
            res = g_JavaVirtualMachine->AttachCurrentThread(&env, NULL);
        }

        /* Get Class from Java **/
        jclass localClass = env->FindClass("com/fido/android/framework/service/XMLDOMDocument");
        if (localClass != NULL) {
            m_XMLDocumentClass = env->NewGlobalRef(localClass);
            /* Call java class constructor. **/
            jmethodID constructor = env->GetMethodID(localClass, "<init>", "()V");
            jobject localObject = env->NewObject(m_XMLDocumentClass , constructor);    
            m_XMLDocumentObject = env->NewGlobalRef(localObject );
        }

    }

    bool Initialize()
    {

    }

    private:
        jclass  m_XMLDocumentClass;
        jobject m_XMLDocumentObject;


};

Questions

  1. Is it right to set JNI interface pointer (JNIEnv* m_JavaEnv) in the constructor once and use it in the whole code ?
  2. Is it right to set jclass m_XMLDocumentClass in the constructor and than use that variable in the all methods ?
  3. Is it right to set jobject m_XMLDocumentObject in the constructor in this way m_JavaEnv->NewObject(m_XMLDocumentClass , constructor); or maybe I must call NewGlobalRef.
  4. What problems can appear if my application work not in the same thread (use many threads) ?
  • 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-05T11:19:09+00:00Added an answer on June 5, 2026 at 11:19 am

    Is it right to set JNI interface pointer (JNIEnv* m_JavaEnv) in the constructor once and use it in the whole code ?

    No. It is thread-specific. That’s what Attach/DetachCurrentThread are for. The only way this can work is if the C++ object is constructed and destroyed in the same thread.

    Is it right to set jclass m_XMLDocumentClass in the constructor and than use that variable in the all methods ?

    No. It is a local reference, and it expires when the JNI method it was acquired in returns. You must save as a global or weak reference unless it is only going to be used within a single JNI method.

    Is it right to set jobject m_XMLDocumentObject in the constructor in this way m_JavaEnv->NewObject(m_XMLDocumentClass , constructor);
    

    No: see above.

    or maybe I must call NewGlobalRef.

    Yes, as above.

    What problems can appear if my application work not in the same thread (use many threads) ?

    Mainly JVM crashes. The JVM assumes you follow all the rules in the JNI specification. So do that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a django application I have the following model: class Appointment(models.Model): #some other fields
I'm writing an application on qooxdoo. I have a class, which should return table
In my application I parsed the data through NSXMLParser and made separate class to
My application has a utility class with static methods which are responsible for showing
I wrote an application in C++ which generates an XML file out of class
I have a class Products, which has the attributes name, description and price. I
My application is throwing a Security Exception with the following details: Description: The application
foreach($apps as $app){ echo $this->Form->input('Application', array('type'=>'checkbox', 'id'=>$app['Application']['description'], 'div'=>false,'type'=>'checkbox','value' => $app['Application']['description'],'label'=>$app['Application']['description'])); } <div class=checkboxes> <input
Description: I am analysing my application to improve the overall performance, and among the
For each item in my application's settings, I've added text to its Description Property

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.