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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:13:33+00:00 2026-05-26T08:13:33+00:00

I am having a problem with JNI, calling a method from C++ to Java.

  • 0

I am having a problem with JNI, calling a method from C++ to Java.

I am trying to call a void method that takes a boolean. My java code is the following:

public void setStatus(boolean bool) {
    // Do stuff...
}

public native void initialize(int defaultPort);

In my C++ code, I am making a struct to hold the env and object and pass it to a thread:

JNIEXPORT void JNICALL Java_com_device_client_HostConnection_initialize
  (JNIEnv * env, jobject obj, jint port)
{
    struct javaInfo* data = (struct javaInfo*) malloc(sizeof(struct javaInfo));
    data->env = env;
    data->javaObjHost = obj;

    pthread_t pth;
    pthread_create(&pth, NULL, startServer, (void *) data);

    free(data);
}

In the actual function, I am trying to obtain the class and then the MethodID and then call the void method, as follows:

void *startServer(void* arg) {
    struct javaInfo* data = (struct javaInfo*) arg; 
    JNIEnv* env = data->env;
    jobject javaObjHost = data->javaObjHost;

    cls = env->GetObjectClass(javaObjHost);
    mid = env->GetMethodID(cls, "setStatus", "(Z)V");
    if (mid == 0) {
        exit(-1);
    }
    env->CallVoidMethod(javaObjHost, mid, true);
}

It is hard for me to debug with JNI. I have tried putting a breakpoint in Eclipse in setStatus() but it never gets called. exit() is not called as well. The programs stomps for a second or two, then continues. I am not sure what is going on.

Could anyone please help me?

Thank you very much.

  • 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-26T08:13:34+00:00Added an answer on May 26, 2026 at 8:13 am

    You cannot pass env pointers to other threads. You need to join the thread to the JVM.

    In the original thread, called GetJavaVM to obtain a JavaVM pointer:

    JavaVM *vm = 0;
    env->GetJavaVM(&vm);
    

    Then in the other thread, attach the VM to that thread and get a new env pointer:

    vm->AttachCurrentThread(&env, 0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I'm having problem with initialization in java, following code give me compile error
am having problem in Saving some data in my Java code. I have like
I am having problem with calling both Parallel::ForkManager and Inline::Java at the same time.
I'm having problem in the following line: rd.PrintOptions.PaperSize = PaperSize.PaperFanfoldStdGerman; it throws an exception
We are having problem with the server migration. We have one application that are
Having problem with this bit of code qith jQuery. it should pick the values
m having problem in passing parameter to controller action, i have done the following
I am having problem with lucene boosting, Iam trying to boost a particular document
We are having nondeterministic System.AccessViolationException thrown from native code. It's hard to reproduce it,
I'm having problem at mipmapping the textures on different hardware. I use the following

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.