env-> findClass的( “java.lang.Math中”);

时间:2011-10-02 16:26:25

标签: java c

env->FindClass("java.lang.Math");失败。为什么呢?

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(mathClass, cosMethod, jIn);
    printf("jOut: %f", jOut);

    printf("DestroyJavaVM.\n");
    jvm->DestroyJavaVM(); 
    printf("END.\n");

    return 0;
}

2 个答案:

答案 0 :(得分:6)

你应该打电话:

jclass mathClass = env->FindClass("java/lang/Math"); 

来自documentation

  

名称:完全限定的类名(即包名,以“/”分隔,后跟类名)。如果名称以“[”(数组签名字符)开头,则返回数组类。

答案 1 :(得分:2)

尝试:

 env->FindClass("java/lang/Math")