Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java C+;JNI:CallStaticCharMethod导致错误_Java_C_Java Native Interface - Fatal编程技术网

Java C+;JNI:CallStaticCharMethod导致错误

Java C+;JNI:CallStaticCharMethod导致错误,java,c,java-native-interface,Java,C,Java Native Interface,我正在尝试将Java和C程序与JNI结合起来。我的目标是将一个字符串从JNI传递到C。当调用CallStaticCharMethod时,我得到一个错误(分段错误?)。我觉得我真的错过了怎么做的要点。我错过了什么 我修改的示例来自 helloWorld.java public class helloWorld{ public static void main(String[] args){ System.out.println("Hello, World"); } public s

我正在尝试将Java和C程序与JNI结合起来。我的目标是将一个字符串从JNI传递到C。当调用CallStaticCharMethod时,我得到一个错误(分段错误?)。我觉得我真的错过了怎么做的要点。我错过了什么

我修改的示例来自

helloWorld.java

public class helloWorld{
    public static void main(String[] args){
    System.out.println("Hello, World");
}
public static char hello(){
        String s = "H";
        char c = s.charAt( 0 );
        return c;
    }
}
hello\u world.c

JNIEnv *create_vm(JavaVM **jvm) {
    JNIEnv *env;
    JavaVMInitArgs args;
    JavaVMOption options;
    args.version = JNI_VERSION_1_6;
    args.nOptions = 1;
    options.optionString = "-Djava.class.path=./";
    args.options = &options;
    args.ignoreUnrecognized = 0;
    int rv;
    rv = JNI_CreateJavaVM(jvm, (void **) &env, &args);
    if (rv < 0 || !env)
        printf("Unable to Launch JVM %d\n", rv);
    else
        printf("Launched JVM! :)\n");
    return env;
}

void invoke_class(JNIEnv *env) {
    jclass hello_world_class;
    jmethodID main_method;
    jmethodID hello_method;
    jint number = 20;
    jint exponent = 3;   
    hello_method = (*env)->GetStaticMethodID(env, hello_world_class, "hello", "()C");

    //This line causes the error:
    (*env)->CallStaticCharMethod(env, hello_world_class, hello_method);
}
int main(int argc, char **argv) {
    JavaVM *jvm;
    JNIEnv *env;
    env = create_vm(&jvm);
    if (env == NULL)
        return 1;
    invoke_class(env);
    return 0;
    }

您需要像上面建议的那样初始化
hello\u world\u类
。注意,
hello\u world\u类
不是helloWorld类的实例。它是对类的引用——这意味着对类型的引用。您的C代码只知道方法名,但不知道类名。方法
hello
可以在很多类中定义,C代码必须知道它是什么类:

hello_world_class = (*env)->FindClass(env, "helloWorld");
hello_method = (*env)->GetStaticMethodID(env, hello_world_class, "hello", "()C");

在这种情况下,不需要全局引用。简单的
FindClass
就足够了。

方法签名不应该是
()C
?我也尝试了Ljava/lang/Character和Ljava/lang/C,但没有帮助。但这可能是错误的,但还有另一个错误…
char
是一个基本类型,因此您不应该在签名中使用
Ljava/lang
。这只是
C
。谢谢你们,我把它改成了()C。我仍然得到了错误,它现在被添加到了问题中。是的,像下面这样得到这个类。我不确定类是否需要转换为全局引用,但为了安全起见,我使用所有JNI对象进行转换:jclass cls=env->FindClass(className);jclass globalCls=静态_cast(env->NewGlobalRef(cls));env->DeleteLocalRef(cls);
hello_world_class = (*env)->FindClass(env, "helloWorld");
hello_method = (*env)->GetStaticMethodID(env, hello_world_class, "hello", "()C");