Calling A JAVA Method From C++ With JNI, No Parameters
Please bear with me, I am an iPhone developer and this whole android this confuses me a bit. I have some c++ methods that are called from a cocos2d-x CCMenuItem. Therefore I canno
Solution 1:
If you are looking at how to call a java method which does not take in any arguments, the format is jmethodID mid = env->GetStaticMethodID(myClass, "myMethod", "()V");
() is how you tell it does not take in any params.
Vsays it returns void. Ljava/lang/String;should be used if the method returns an object of type String.
Solution 2:
A number of things...
- Given the declaration
JNIEnv* env;, and given that you're in C++, you use it asenv->FindClass(someString), not how you're doing it. If it was C you'd useFindClass(env, someString)but in C++ you useenv->FindClass(someString). - The string to use in
FindClassis the fully qualified path name but with/as the delimiter instead of.For example, if the class isFooin packagebar.baz.quux, the fully-qualified name isbar.baz.quux.Fooand the string you'd give toFindClassisbar/baz/quux/Foo. - You can only create one JVM per C++ process. I'm pretty sure you need to create a single JVM one time. You will therefore need to have the
JavaVM* vmbe a global variable (or at least be somewhere accessible to everything that needs to use. Everything in the same C++ thread as the thread that calledJNI_CreateJavaVM()will use theJNIEnv *that gets filled in by that call. Every other thread that wants to use the JVM needs to callAttachCurrentThreadwhich will bind that thread to the JVM and fill in a newJNIEnv *valid for that thread. - Have you double-checked your compiler/IDE settings to make sure that the
JDK_HOME/includedirectory (which containsjni.h) is in the includes search path? Same for theJDK_HOME/include/androiddirectory (or whatever the operating specific directory inJDK_HOME/includeis called in a Android JDK)?
A very useful resource is The JNI book
But be careful while reading it because some examples are in C and some are in C++, so make sure you understand how the calling conventions differ.
Post a Comment for "Calling A JAVA Method From C++ With JNI, No Parameters"