#include <jni.h> #include <stdio.h> #include <stdlib.h> #include <wchar.h> /* include the file to support unicode function */ const char* DispatchStatus(int status) { switch (status) { case JNI_OK: return /"success/"; break; case JNI_ERR: return /"unknown error/"; break; case JNI_EDETACHED: return /"thread detached from the VM/"; break; case JNI_EVERSION: return /"JNI version error/"; break; default: return __FILE__ /": unknow error/"; break; } } void ArrayTest(JNIEnv* env) { int i = 1; jintArray array = (*env)-> NewIntArray(env, 10); for(; i<= 10; i++) (*env)->SetIntArrayRegion(env, array, i-1, 1, &i); /* 获取数组对象的元素个数 */ int len = (*env)->GetArrayLength(env, array); /* 获取数组中的所有元素 */ jint* elems = (*env)-> GetIntArrayElements(env, array, 0); for(i=0; i< len; i++) printf(/"ELEMENT %d IS %d//n/", i, elems[i]); } int main(int argc, const char** argv) { jint status = JNI_OK; JavaVM* jvm = (JavaVM*)0; JNIEnv* env = (JNIEnv*)0; JavaVMInitArgs vm_args; vm_args.version = JNI_VERSION_1_4; vm_args.options = (JavaVMOption*)0; vm_args.nOptions = 0; vm_args.ignoreUnrecognized = JNI_FALSE; status = JNI_GetDefaultJavaVMInitArgs(&vm_args); if (0 != status) { printf(/"%s//n/", DispatchStatus(status)); return EXIT_FAILURE; } status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args); if (0 != status) { printf(/"test: %s//n/", DispatchStatus(status)); return EXIT_FAILURE; } printf(/"//r//n===== Array testing =====//r//n/"); ArrayTest(env); printf(/"test: success//n/"); if(jvm) (*jvm)->DestroyJavaVM(jvm); return EXIT_SUCCESS; } //该片段来自于http://outofmemory.cn
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/10428.html