gpt4 book ai didi

java - 在android中通过jni调用c中的Main

转载 作者:太空狗 更新时间:2023-10-29 14:14:40 24 4
gpt4 key购买 nike

我想调用main:

int main(int argc, char *argv[])

我试过这个链接:How to use JNI to call a "main" function that takes an array of character pointers?

JNIEXPORT jint JNICALL Java_package_Class_lameMain(JNIEnv *env, jclass class,  jobjectArray jargv)
{ //jargv is a Java array of Java strings
int argc = env->GetArrayLength(jargv);
typedef char *pchar;
pchar *argv = new pchar[argc];
int i;
for(i=0; i<argc; i++)
{
jstring js = env->GetObjectArrayElement(jargv, i); //A Java string
const char *pjc = env->GetStringUTFChars(js); //A pointer to a Java-managed char buffer
size_t jslen = strlen(pjc);
argv[i] = new char[jslen+1]; //Extra char for the terminating null
strcpy(argv[i], pjc); //Copy to *our* buffer. We could omit that, but IMHO this is cleaner. Also, const correctness.
env->ReleaseStringUTFChars(js, pjc);
}

//Call main
main(argc, argv);

//Now free the array
for(i=0;i<argc;i++)
delete [] argv[i];
delete [] argv;
}

但它不起作用,因为我在 C 而不是 C++ 上编程。在我做了一些更改后,“GetArrayLength”出现了这个错误:

error: request for member 'GetArrayLength' in something not a structure or union

我也试过这个链接:How to call the main function in C file by JNI in Android?

 JNIEXPORT return_type JNICALL Java_full_package_name_classname_functionname(JNIEnv *env, jobject object, Jobject stringArray){
// your argc
int size = env->GetArrayLength(stringArray);
char **argv = new char*[size];
for (int i=0; i < size; ++i)
{
jstring string = env->GetObjectArrayElement(stringArrays, i);
const char* cString = env->GetStringUTFChars(string, 0);
argv[i] = strdup(cString);
env->ReleaseStringUTFChars(string, myarray);
env->DeleteLocalRef(string);
}
// you will need to modify main so it does properly cleanup
main(argc, argv);
// clean up
for(int i = 0; i < size; ++i)
free(argv[i]);
delete [] argv;
}

错误是:预期的“;”或 ',' befor Java_package_Class_lameMain

最佳答案

像这样对你的函数进行 repdect 操作:

JNIEXPORT void JNICALL Java_ClassName_MethodName
(JNIEnv *env, jobject obj, jstring javaString)
{

const char *nativeString = (*env)->GetStringUTFChars(env, javaString, 0);

(*env)->ReleaseStringUTFChars(env, javaString, nativeString);
}

事情是使用*env

请参阅此处的 C 示例 http://en.wikipedia.org/wiki/Java_Native_Interface

关于java - 在android中通过jni调用c中的Main,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23605169/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com