How to pass String[] from java to C++ using JNI

I am writing a java console application. while running that needs to pass some arguments from command prompt. So i need to process those all arguments in c++ code using JNI.

The following is my JavaCode:

class RetArray{

public native int readArray(String[] argsArr);

static{

System.loadLibrary("readArray");

}

public static void main (String[] args){

RetArray objRetArr = new RetArray();

String[] argu = new String[5];

int i = objRetArr.readArray(argu);

}

}

can u plz provide me the code in c++ ?

By the way I tried to write the code in c++ too as following:

#include <jni.h>

#include "RetArray.h"

#include <stdio.h>

#include <iostream.h>

JNIEXPORT jint JNICALL Java_RetArray_readArray(JNIEnv *env, jobject obj, jobjectArray myArray){

jint arrsize=env->GetArrayLength(env, myArray);

jobjectArray retArr = (jobjectArray)env->NewObjectArray(arrsize, env->FindClass("java/lang/String"), env->NewString(""));

for(jint i=0; i<arrsize;i++){

retArr=env->GetObejctArrayElement(myArray,i);

//printf("%c\n", retArr);

cout << retArr;

}

return 1;

}

but while making "so", its giving me lots of error.

if ne answer then do inform me at abhiforu2002@sify.com. As its very urgent for me. Thanx in Advance.

[1454 byte] By [abheeea] at [2007-10-2 20:16:52]
# 1
http://forum.java.sun.com/thread.jspa?threadID=734674Try to use search before asking...
Michael.Nazarov@sun.coma at 2007-7-13 22:59:16 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2

If arr is String[] then something like this:

int iSize = env->GetArrayLength( arr );

for( int i = 0; i < iSize; i++ )

{

jobject o = env->GetObjectArrayElement( arr, i );

const unsigned short* sz = env->GetStringChars( ( jstring )o, false );

wprintf( L"%s\n", sz );

env->ReleaseStringChars( ( jstring )o, sz );

}

Thanx Michael!!!

Above code is working.......but still one problem.

If i pass "Hello" and "World" as element of string Array to the above code then its printing "H" and "W" only. Not the whole word.

So plz help me again.

Message was edited by: self

abheee

abheeea at 2007-7-13 22:59:16 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3
Looks like you treat your strings as normal "char" while they are unicode "short", so there is x00 after each character (in terms of "char" strings).Review your code and my, or post your code. I think there are differences :)
Michael.Nazarov@sun.coma at 2007-7-13 22:59:16 > top of Java-index,Java HotSpot Virtual Machine,Specifications...