[Last Call] Learn how to a build a cloud-first strategyRegister Now


Return a String[][] to Java from C

Posted on 2005-04-13
Medium Priority
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
  • 2
LVL 15

Expert Comment

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

see: http://java.sun.com/docs/books/jni/html/objtypes.html (Accessing Arrays of Objects)

Author Comment

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

Thanks any way.
LVL 15

Accepted Solution

aozarov earned 600 total points
ID: 13782691
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);

see: http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jnistring.html#multi

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month18 days, 11 hours left to enroll

834 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