有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

c env>FindClass(“java.lang.Math”);

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) 个答案

  1. # 1 楼答案

    您应该拨打:

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

    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.

  2. # 2 楼答案

    尝试:

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