错误:' - >'的基本操作数有非指针类型'JNIEnv'

Ror*_*ach 30 java java-native-interface

#include <stdio.h>
#include <jni.h>

 JNIEnv* create_vm() {
    JavaVM* jvm;
    JNIEnv* env;
    JavaVMInitArgs args;
    JavaVMOption options[1];

    /* There is a new JNI_VERSION_1_4, but it doesn't add anything for the purposes of our example. */
    args.version = JNI_VERSION_1_2;
    args.nOptions = 1;
    options[0].optionString = "-Djava.class.path=/home/test/workspace/pankajs/"
            "jikes/JikesRVMia32-linuxproduction/target/tests/stress/prototype/basic/classes";
    args.options = options;
    args.ignoreUnrecognized = JNI_FALSE;

    JNI_CreateJavaVM(&jvm, (void **)&env, &args);
    return env;
}

void invoke_class(JNIEnv* env) {
    jclass helloWorldClass;
    jmethodID mainMethod;
    jobjectArray applicationArgs;
    jstring applicationArg0;

    helloWorldClass = (*env)->FindClass(env, "/test/org/jikesrvm/basic/core/bytecode/TestSwitch");

    mainMethod = (*env)->GetStaticMethodID(env, helloWorldClass, "main", "([Ljava/lang/String;)V");

    applicationArgs = (*env)->NewObjectArray(env, 1, (*env)->FindClass(env, "java/lang/String"), NULL);
    applicationArg0 = (*env)->NewStringUTF(env, "From-C-program");
    (*env)->SetObjectArrayElement(env, applicationArgs, 0, applicationArg0);

    (*env)->CallStaticVoidMethod(env, helloWorldClass, mainMethod, applicationArgs);
}


int main(int argc, char **argv) {
    JNIEnv* env = create_vm();
    invoke_class( env );
}

I am trying to learn how JVM is actually invoked from bootstrap C files.
Run Code Online (Sandbox Code Playgroud)

我在互联网上找到了这个例子,我在运行它时遇到了一些问题.

据我所知,我正确指定了构建命令,

  g++ -g -I /usr/lib/jvm/java-6-sun-1.6.0.26/include -I /usr/lib/jvm/java-6-sun-1.6.0.26/include/linux CallJVM.c
Run Code Online (Sandbox Code Playgroud)

我的意图是使用jikesrvm实际运行它,但为了试验这个我选择使用JVM.我得到的错误是:

CallJVM.c: In function ‘JNIEnv* create_vm()’:
CallJVM.c:14:4: warning: deprecated conversion from string constant to ‘char*’
CallJVM.c: In function ‘void invoke_class(JNIEnv*)’:
CallJVM.c:28:26: error: base operand of ‘->’ has non-pointer type ‘JNIEnv’
CallJVM.c:30:21: error: base operand of ‘->’ has non-pointer type ‘JNIEnv’
CallJVM.c:32:26: error: base operand of ‘->’ has non-pointer type ‘JNIEnv’
CallJVM.c:32:57: error: base operand of ‘->’ has non-pointer type ‘JNIEnv’
CallJVM.c:33:26: error: base operand of ‘->’ has non-pointer type ‘JNIEnv’
CallJVM.c:34:8: error: base operand of ‘->’ has non-pointer type ‘JNIEnv’
CallJVM.c:36:8: error: base operand of ‘->’ has non-pointer type ‘JNIEnv’
Run Code Online (Sandbox Code Playgroud)

我注意到在C和C++中实现的不同方式,但我认为我正在正确地编写它.

编辑:在使用gcc进行编译时,我得到了

undefined reference to `JNI_CreateJavaVM'
Run Code Online (Sandbox Code Playgroud)

这是在eclipse中被提示但我认为我的配置不合适.当我使用ctrl+click它时,我也引用了jni.h,但仍然缺少参考的原因?我特意提到包含路径上的文件夹.

And*_*ter 78

我注意到在C和C++中实现的不同方式,但我认为我正在正确地编写它.

您正在使用C变体,但正在编译g++,它会调用C++编译器(即使您的源文件有.c扩展名).

要么改变C像这样的变体

(*env)->FindClass(env, ...)
Run Code Online (Sandbox Code Playgroud)

C++变种一样,

env->FindClass(...)
Run Code Online (Sandbox Code Playgroud)

或者将编译器切换gcc为编译源C代码.

  • [除了几种情况](http://stackoverflow.com/questions/1201593/c-subset-of-c-where-not-examples),它可以 - 但这里的问题是`jni`头文件假定由 C++ 编译器编译时为“C++”代码,因此提供“C++”风格 API,该 API 与“C”风格 API 不兼容。查看 `jni.h` - 你会发现很多 `#ifdef __cplusplus` 块 (2认同)