env->FindClass("java.lang.Math");
fails. Why?
gcc -I/System/Library/Frameworks/JavaVM.framework/Headers test.cpp -framework JavaVM -o test && ./test
http://developer.apple.com/library/mac/#samplecode/simpleJavaLauncher/Listings/utils_h.html#//apple_ref/doc/uid/DTS10000688-utils_h-DontLinkElementID_7 http://developer.apple.com/library/mac/#technotes/tn2147/_index.html
#include <jni.h>
#include <stdlib.h>
int main() {
printf("START.\n");
JavaVM* jvm = NULL;
JNIEnv *env;
JavaVMInitArgs vm_args;
JNI_GetDefaultJavaVMInitArgs(&vm_args);
vm_args.version = JNI_VERSION_1_6;
vm_args.nOptions = 0;
int ret = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
if(ret < 0) {
printf("Unable to Launch JVM\n");
return 1;
}
jclass mathClass = env->FindClass("java.lang.Math");
if (mathClass == NULL) {
printf("Unable to find java.lang.Math\n");
return 1;
}
jmethodID cosMethod = env->GetStaticMethodID(mathClass, "cos", "(D)D");
if (cosMethod == NULL) {
printf("Unable to find java.lang.Math.cos()\n");
return 1;
}
printf("call\n");
jdouble jIn = 0.1;
jdouble jOut = env->CallStaticIntMethod(math开发者_如何学JAVAClass, cosMethod, jIn);
printf("jOut: %f", jOut);
printf("DestroyJavaVM.\n");
jvm->DestroyJavaVM();
printf("END.\n");
return 0;
}
You should be calling:
jclass mathClass = env->FindClass("java/lang/Math");
From the documentation:
name: fully-qualified class name (that is, a package name, delimited by “/”, followed by the class name). If the name begins with “[“ (the array signature character), it returns an array class.
Try:
env->FindClass("java/lang/Math")
精彩评论