This is the mail archive of the
java@gcc.gnu.org
mailing list for the Java project.
Re: Converting JString array into char*[]
- To: Bryan Ha <kfh1_99 at yahoo dot com>
- Subject: Re: Converting JString array into char*[]
- From: Tom Tromey <tromey at redhat dot com>
- Date: 17 Aug 2001 09:19:58 -0600
- Cc: java at gcc dot gnu dot org
- References: <20010817134149.18097.qmail@web11001.mail.yahoo.com>
- Reply-To: tromey at redhat dot com
>>>>> "Bryan" == Bryan Ha <kfh1_99@yahoo.com> writes:
Bryan> My aim is to convert String[] into char*[] which can
Bryan> be used by the system call execve. Does this work ?
Bryan> And is this the preferred way ?
Bryan> jint Hello::process(java::lang::String[] arguments,
Bryan> java::lang::String[] environ)
Arrays are handled differently in CNI. You can't use [] because a
Java array is not a C++ array -- it is a kind of structure.
jint Hello::process (jstringArray arguments, jstringArray environ)
Bryan> char* args[arguments.length];
Bryan> char* envp[environ.length];
Use `arguments->length'. A Java reference is a C++ pointer.
Bryan> jstring arg=arguments[i];
Bryan> jstring env=environ[i];
Unfortunately this doesn't work. `operator[]' isn't defined in C++
for Java arrays.
Outside the loop write:
jstring *argElts = elements (arguments);
Then inside the loop write:
jstring arg = argElts[i];
Bryan> l2=JvGetStringUFTLength(env);
Bryan> JvGetStringUTFRegion(env,0,l1,envp[i]);
You want `l2' here.
And, you want to allocate space for the result.
For instance:
jsize s = JvGetStringUTFLength (string);
char *buf = (char *) JvMalloc (s + 1);
JvGetStringUTFRegion (string, 0, s, buf);
buf[s] = '\0';
Bryan> execve(cmd,args,envp);
Don't you need to NULL-terminate the arrays?
Tom