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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:46:57+00:00 2026-05-20T21:46:57+00:00

I have called a native program which creates another thread, which attaches itself to

  • 0

I have called a native program which creates another thread, which attaches itself to the JVM. Now I want to access the methods of the JVM, but it fails. Here is the code:

//
// This is the native function that gets called first. 
// it creates another thread which runs, and also calls the printing-methods in my
// java applet. 
//
JNIEXPORT void JNICALL Java_EIGC_1Applet_app_1native_native_1start(JNIEnv* jenv, jobject job) {

    printAppletConsole(jenv,job,"unused atm");
    // save current java VM;
    // save main applet class;
    // used by main thread
    jenv->GetJavaVM(&applet_java_jvm);
    m_job = job;


    // create the working and start it
    applet_thread_main = new EIGC_Applet_thread(&main);
    applet_thread_main->start();
}


//
// This is the running thread that was created
// This will run and call the printing method in the applet
//
unsigned __stdcall main(void* args) {
    // The JNIEnv
    JNIEnv* jenv = new JNIEnv();

    // attach thread to running JVM
    applet_java_jvm->AttachCurrentThread((void**)jenv,NULL);

    // main running loop
    while (true) {
         Sleep(1000);
         printAppletConsole(jenv,m_job,"unused");
    }

    applet_thread_main->stop();
    return 0;
    }


//
// Calls the "writeConsole()" method in applet which prints "test" in a JTextArea
//
void printAppletConsole(JNIEnv* jenv,jobject job,char* text) {
    jclass cls = jenv->GetObjectClass(job);
    jmethodID mid = jenv->GetMethodID(cls,"writeConsole","()V");
    if (mid==NULL) { 
            printf("Method not found");
    }
    else {
        jenv->CallVoidMethod(job,mid);
    }
}

I have one question;

1) In the newly created thread, the JVM just hangs when I try to call printAppletConsole, it hangs on the GetObjectClass(). Why is this?

My suspicion is that since I have created a new thread, I need to access a new instance of jobject, but Im not sure how..

Thanks!

  • 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-20T21:46:58+00:00Added an answer on May 20, 2026 at 9:46 pm
    m_job = job;
    

    This just keeps the local reference which is invalid as soon as you return to java. You need to make a global referene with NewGlobalRef and store that.

    JNIEnv* jenv = new JNIEnv();
    applet_java_jvm->AttachCurrentThread((void**)jenv,NULL);
    

    Should be:

    JNIEnv* jenv = 0:
    applet_java_jvm->AttachCurrentThread(&jenv,NULL);
    

    EDIT: For older JNI versions, use:

    JNIEnv* jenv = 0:
    applet_java_jvm->AttachCurrentThread((void **) &jenv,NULL);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my application I have a wrapper over some native code, which is called
I wants to access a method in different project. as example i have called
i have table called as Support, which have a field named Name and contains
I have a native C++ ATL in-proc COM server. A separate test program calls
I have got the following code in a file called test.java which is located
I have a class called communicator. This class is a listener of a thread
I have a Vendor.DLL (Native DLL, written in C++) that exposes many methods. Typically
I have Class in java called XMLDOMDocument this class have some methods CreateXML ,
I have a java script function that i have called on body load mousemove()
Using a .NET TcpClient if I have called an asynchronous BeginRead() on the associated

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.