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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:00:59+00:00 2026-05-12T05:00:59+00:00

JNI tutorials, for instance this one, cover quite well how to access primitive fields

  • 0

JNI tutorials, for instance this one, cover quite well how to access primitive fields within an object, as well as how to access arrays that are provided as explicit function arguments (i.e. as subclasses of jarray). But how to access Java (primitive) arrays that are fields within an jobject? For instance, I’d like to operate on the byte array of the following Java object:

class JavaClass {
  ...
  int i;
  byte[] a;
}

The main program could be something like this:

class Test {

  public static void main(String[] args) {
    JavaClass jc = new JavaClass();
    jc.a = new byte[100];
    ...
    process(jc);
  }

  public static native void process(JavaClass jc);
}

The corresponding C++ side would then be:

JNIEXPORT void JNICALL Java_Test_process(JNIEnv * env, jclass c, jobject jc) {

  jclass jcClass = env->GetObjectClass(jc);
  jfieldID iId = env->GetFieldID(jcClass, "i", "I");

  // This way we can get and set the "i" field. Let's double it:
  jint i = env->GetIntField(jc, iId);
  env->SetIntField(jc, iId, i * 2);

  // The jfieldID of the "a" field (byte array) can be got like this:
  jfieldID aId = env->GetFieldID(jcClass, "a", "[B");

  // But how do we operate on the array???
}

I was thinking to use GetByteArrayElements, but it wants an ArrayType as its argument. Obviously I’m missing something. Is there a way to to this?

  • 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-12T05:00:59+00:00Added an answer on May 12, 2026 at 5:00 am

    I hope that will help you a little (check out the JNI Struct reference, too):

    // Get the class
    jclass mvclass = env->GetObjectClass( *cls );
    // Get method ID for method getSomeDoubleArray that returns a double array
    jmethodID mid = env->GetMethodID( mvclass, "getSomeDoubleArray", "()[D");
    // Call the method, returns JObject (because Array is instance of Object)
    jobject mvdata = env->CallObjectMethod( *base, mid);
    // Cast it to a jdoublearray
    jdoubleArray * arr = reinterpret_cast<jdoubleArray*>(&mvdata)
    // Get the elements (you probably have to fetch the length of the array as well
    double * data = env->GetDoubleArrayElements(*arr, NULL);
    // Don't forget to release it 
    env->ReleaseDoubleArrayElements(*arr, data, 0); 
    

    Ok here I operate with a method instead of a field (I considered calling a Java getter cleaner) but you probably can rewrite it for the fields as well. Don’t forget to release and as in the comment you’ll probably still need to get the length.

    Edit: Rewrite of your example to get it for a field. Basically replace CallObjectMethod by GetObjectField.

    JNIEXPORT void JNICALL Java_Test_process(JNIEnv * env, jclass c, jobject jc) {
    
      jclass jcClass = env->GetObjectClass(jc);
      jfieldID iId = env->GetFieldID(jcClass, "i", "I");
    
      // This way we can get and set the "i" field. Let's double it:
      jint i = env->GetIntField(jc, iId);
      env->SetIntField(jc, iId, i * 2);
    
      // The jfieldID of the "a" field (byte array) can be got like this:
      jfieldID aId = env->GetFieldID(jcClass, "a", "[B");
    
      // Get the object field, returns JObject (because Array is instance of Object)
      jobject mvdata = env->GetObjectField (jc, aID);
    
      // Cast it to a jdoublearray
      jdoubleArray * arr = reinterpret_cast<jdoubleArray*>(&mvdata)
    
      // Get the elements (you probably have to fetch the length of the array as well  
      double * data = env->GetDoubleArrayElements(*arr, NULL);
    
      // Don't forget to release it 
      env->ReleaseDoubleArrayElements(*arr, data, 0);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You probably want to call setlocale() first, "LC_ALL" should do… May 14, 2026 at 9:06 am
  • Editorial Team
    Editorial Team added an answer Linux Ubuntu Desktop Jaunty Firebug FireCookie Pixel Perfect Web developer… May 14, 2026 at 9:06 am
  • Editorial Team
    Editorial Team added an answer Your code should look like this: var par = [];… May 14, 2026 at 9:06 am

Related Questions

I've been searching for a while and everybody seems to think this is not
I want to create native Mac OS X application using Cocoa + Objective C
I like to generate a thread dump programmatically. I've learned that there a basically
i use the command line in windows to compile and then execute my java

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.