How to Return an ArrayList of Strings from Native Java to JNI

Question

How can I return an ArrayList of Strings from Java to JNI?

JNIEXPORT jobject JNICALL Java_MyClass_getStringList(JNIEnv *env, jobject obj) {
    // Create a new Java ArrayList
    jclass arrayListClass = (*env)->FindClass(env, "java/util/ArrayList");
    jmethodID arrayListInit = (*env)->GetMethodID(env, arrayListClass, "<init>", "()V");
    jobject arrayList = (*env)->NewObject(env, arrayListClass, arrayListInit);

    // Add Strings to the ArrayList
    jmethodID arrayListAdd = (*env)->GetMethodID(env, arrayListClass, "add", "(Ljava/lang/Object;)Z");
    jclass stringClass = (*env)->FindClass(env, "java/lang/String");
    jstring str1 = (*env)->NewStringUTF(env, "Hello");
    (*env)->CallBooleanMethod(env, arrayList, arrayListAdd, str1);
    jstring str2 = (*env)->NewStringUTF(env, "World");
    (*env)->CallBooleanMethod(env, arrayList, arrayListAdd, str2);

    // Return the ArrayList
    return arrayList;
}

Answer

JNI provides a way for Java code to call and be called by native applications and libraries written in other languages such as C and C++. Returning an ArrayList of Strings from native code back to a Java application involves creating an ArrayList instance and adding elements to it through JNI calls.

JNIEXPORT jobject JNICALL Java_MyClass_getStringList(JNIEnv *env, jobject obj) {
    // Create a new Java ArrayList
    jclass arrayListClass = (*env)->FindClass(env, "java/util/ArrayList");
    jmethodID arrayListInit = (*env)->GetMethodID(env, arrayListClass, "<init>", "()V");
    jobject arrayList = (*env)->NewObject(env, arrayListClass, arrayListInit);

    // Add Strings to the ArrayList
    jmethodID arrayListAdd = (*env)->GetMethodID(env, arrayListClass, "add", "(Ljava/lang/Object;)Z");
    jclass stringClass = (*env)->FindClass(env, "java/lang/String");
    jstring str1 = (*env)->NewStringUTF(env, "Hello");
    (*env)->CallBooleanMethod(env, arrayList, arrayListAdd, str1);
    jstring str2 = (*env)->NewStringUTF(env, "World");
    (*env)->CallBooleanMethod(env, arrayList, arrayListAdd, str2);

    // Return the ArrayList
    return arrayList;
}

Causes

  • Understanding JNI conventions
  • Properly managing Java objects from native code
  • Using the right method IDs to interact with Java classes

Solutions

  • Use the JNI API correctly to access Java class methods and constructors.
  • Manage the local references to prevent memory leaks in JNI.

Common Mistakes

Mistake: Not releasing local references, leading to memory leaks.

Solution: Use DeleteLocalRef() after you're done with each local reference.

Mistake: Incorrectly calling Java methods leading to exceptions at runtime.

Solution: Check for exceptions using env->ExceptionCheck() and env->ExceptionDescribe() after native calls.

Helpers

  • JNI
  • Java Native Interface
  • ArrayList
  • C code
  • return Java objects from JNI
  • Java to JNI
  • JNI ArrayList example

Related Questions

⦿How to Use the Google Custom Search API in Java

Learn how to effectively implement the Google Custom Search API in Java with detailed code examples and best practices.

⦿How to Convert a String Containing XML Data into a JDOM Document

Learn how to transform an XML string into a JDOM Document in Java with stepbystep guidance and code examples.

⦿Understanding Eager vs. Lazy Fetching in Hibernate Many-to-Many Relationships

Explore eager and lazy fetching strategies in Hibernate for managing manytomany relationships effectively. Learn best practices and tips.

⦿How to Resolve 'Unmapped Spring Boot Annotated Configuration Classes' Error in IntelliJ?

Learn how to fix the Unmapped Spring Boot Annotated Configuration Classes error in IntelliJ. Follow our expert guide for stepbystep solutions.

⦿How to Use Platform.exit() and System.exit(int) Together in Java?

Learn how to effectively use Platform.exit and System.exitint together in Java applications to manage application termination.

⦿How to Resolve 'Invalid JAVA_HOME' Error When Building with Cordova

Learn how to fix the Invalid JAVAHOME error in Cordova builds with a detailed guide including causes and solutions.

⦿How to Use Spring Data JPA's findBy for Multiple Fields with Containing Clause

Learn how to use Spring Data JPAs findBy method for multiple fields including Containing clauses. Get expert tips and code examples.

⦿How to Retrieve Values from a JTable Cell in Java?

Learn how to effectively retrieve values from JTable cells in Java with stepbystep instructions and code examples.

⦿Why Does the getResource() Method Return Null in JDK 11?

Discover why the getResource method may return null in JDK 11 including common causes and solutions for effective resource management in Java.

⦿How to Use Java to Send Key Combinations Programmatically?

Learn how to programmatically send key combinations in Java using Robot class for efficient automation.

© Copyright 2025 - CodingTechRoom.com