c++ - Java JNI UnsatisfiedLinkError C char pointer to pointer -
hi working on project c++ framework shall integrated java program, started exploring jni. struggling type conversions , argument types should declared in java native method since new c++.
in cpp-file have following method:
using namespace std; int main(int argc,char**argv) { //do stuff }
my corresponding java class looks this:
public class jnitest { static { system.loadlibrary("plain2snt"); } public jnitest() { char[][] arguments = new char[][] {"resourcefile1.txt".tochararray(), "resourcefile2.txt".tochararray()}; main(2, arguments); } public native int main(int argc, char[][] argv); }
and header file java class following:
#include <jni.h> /* header class testpackage_jnitest */ #ifndef _included_testpackage_jnitest #define _included_testpackage_jnitest #ifdef __cplusplus extern "c" { #endif /* * class: testpackage_jnitest * method: main * signature: (i[[c)i */ jniexport jint jnicall java_testpackage_jnitest_main (jnienv *, jobject, jint, jobjectarray); #ifdef __cplusplus } #endif #endif
when try run following error:
exception in thread "main" java.lang.unsatisfiedlinkerror: testpackage.jnitest.main(i[ljava/lang/string;)i @ testpackage.jnitest.main(native method) @ testpackage.jnitest.<init>(jnitest.java:14) @ testpackage.main.main(main.java:6)
i used google, jni api reference, etc. type matching between java , c++ not find how handle char pointer pointer type in java. actual type definition in java class argv argument match char** in cpp framework?
when jvm tries invoke native method - expects you've implemented function pointed @ header generated javah
. i.e. library somewhere must contain
jniexport jint jnicall java_testpackage_jnitest_main (jnienv *, jobject, jint, jobjectarray) { /* stuff */ }
and caught exception claim, there no implementation native method.
next, more convenient pass arguments
not array-of-array-of-char array of string
.
public jnitest() { string[] arguments = new string[] {"resourcefile1.txt", "resourcefile2.txt"}; main(2, arguments); } public native int main(int argc, string[] argv); }
in c++ can obtain args in next way:
jniexport jint jnicall java_testpackage_jnitest_main (jnienv *, jobject thiz, jint argc, jobjectarray argv) { for(jsize = 0; < env->getarraylength(argv); i++) { jstring str = (jstring)env->getobjectarrayelement(argv, i); const char *c_str = env->getstringutfchars(str, null); /* whatever want c_str */ env->releasestringutfchars(str, c_str); } /* ... */ }
Comments
Post a Comment