Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For sure, you'll have to write a C function called by the Java code.</p> <p>As seen in the answer pointed to by Radiodef, that function will receive a <code>jobjectarray</code>, as a java <code>String[]</code> is represented in <code>jni</code> as a <code>jobjectArray</code>.</p> <p>In the function, you will have to malloc a C arrays, for storing all the char* pointers that your legacy <code>main</code> function is expecting in it's <code>char **argv</code> argument.</p> <p>You will store in that array malloced pointers, in order to be able to release the JNI objects immediately. You may avoid those malloc, but at the cost of storing JNI resources in another array for further release, so I don't think it's a goo idea.</p> <p>Remember that the convention for legacy main functions is that the first arg (index 0) is the 'program name'. You will have to fake it.</p> <pre><code>void MyJNIFunction(JNIEnv *env, jobject object, jobjectArray stringArray) { // Get the number of args jsize ArgCount = (*env)-&gt;GetArrayLength(env, stringArray); // malloc the array of char* to be passed to the legacy main char ** argv = malloc(sizeof(char*)*(ArgCount+1)); // +1 for fake program name at index 0 argv[ 0 ] = "MyProgramName"; int i; for ( i = 0; i &lt; ArgCount; ++i ) { jstring string = (jstring)((*env)-&gt;GetObjectArrayElement(env, stringArray, i)); const char *cstring = (*env)-&gt;GetStringUTFChars(env, string, 0); argv[ i + 1 ] = strdup( cstring ); (*env)-&gt;ReleaseStringUTFChars(env, string, cstring ); (*env)-&gt;DeleteLocalRef(env, string ); } // call the legacy "main" function LegacyMain( ArgCount + 1, argv ); // cleanup for( i = 0; i &lt; ArgCount; ++i ) free( argv[ i + 1 ] ); free( argv ); return; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload