This is the mail archive of the java@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Converting JString array into char*[]


>>>>> "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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]