Return a String[][] to Java from C

Posted on 2005-04-13
Last Modified: 2008-02-01
Hi experts,

I have write a C program that is called from Java with JNI. Everything was working rigth but when I change a simple jint return for a jobjectarray return by problem began.

This is the piece of code that I have write to contruct a jobjectarray from the PARAMETROS struct.

struct parametros {
      char clave[MAX_LONGITUD];
      char valor[MAX_LONGITUD];


jobjectArray char2jobjectArray(JNIEnv *env, PARAMETROS* parametrosC, int numParametros) {
    jobjectArray ret,fila;
    int i,j;
    jstring s;
    jclass string_class;
    jclass stringstring_class;
    string_class = (*env)->FindClass(env,"java/lang/String");
    stringstring_class = (*env)->FindClass(env, "(Ljava.lang.String;)");

    ret= (jobjectArray)(*env)->NewObjectArray(env, 5,
    for(i=0;i<5;i++) {
             fila = (*env)->NewObjectArray(env, 2, string_class, NULL);

            s = (*env)->NewStringUTF(env, parametrosC[i].clave);
            (*env)->SetObjectArrayElement(env, fila, j, s);
            (*env)->DeleteLocalRef(env, s);
            s = (*env)->NewStringUTF(env, parametrosC[i].valor);
            (*env)->SetObjectArrayElement(env, fila, j, s);
            (*env)->DeleteLocalRef(env, s);

             (*env)->SetObjectArrayElement(env, ret, i, fila);
         (*env)->DeleteLocalRef(env, fila);

The first thing is that I can't get the class for a String[], so my programs stops there.

Can anyone provide a correction or a new solution for that. My purpose is just to write down a funcion like that:

jobjectArray char2jobjectArray(JNIEnv *env, PARAMETROS* parametrosC, int numParametros)

Question by:elrichal
    LVL 15

    Expert Comment

    Try using
    stringstring_class = (*env)->FindClass(env, "[java/lang/String");
    instead of
    stringstring_class = (*env)->FindClass(env, "(Ljava.lang.String;)");

    see: (Accessing Arrays of Objects)

    Author Comment

    It's not possible to find the String[] class even with your sugestion "[java/lang/String".

    Thanks any way.
    LVL 15

    Accepted Solution

    Then I think you can do it this way:

    jclass string_class = env->FindClass("java/lang/String");
    printf("Class %d", string_class);
    // create a string[5][3] array
    jobjectArray row = env->NewObjectArray(3, string_class, NULL);
    printf("Class %d", row);
    jobjectArray array = env->NewObjectArray(5, env->GetObjectClass(row), NULL);
    printf("Class %d", array);


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now