【问题标题】:error: base operand of ‘->’ has non-pointer type ‘JNIEnv’错误:“->”的基本操作数具有非指针类型“JNIEnv”
【发布时间】:2013-04-02 13:08:54
【问题描述】:
#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.

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

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

  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

我的意图是使用 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’

我注意到在 C 和 C++ 中实现的不同方式,但我认为我写得正确。

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

undefined reference to `JNI_CreateJavaVM'

这就是 Eclipse 中提示的内容,但我认为我的配置不合适。当我使用 ctrl+click 时,它也将我带到引用的 jni.h 但仍然为什么它缺少引用? 我特别提到了我在包含路径上的文件夹。

【问题讨论】:

    标签: java java-native-interface


    【解决方案1】:

    我注意到在 C 和 C++ 中实现的不同方式,但我认为我写得正确。

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

    要么更改C 变体,如

    (*env)->FindClass(env, ...)
    

    C++ 变体,比如

    env->FindClass(...)
    

    或将您的编译器切换到gcc 以将源代码编译为C 代码。

    【讨论】:

    • 谢谢...我一直以为C++编译器也可以编译C代码。
    • Besides a couple of cases,它可以 - 但这里的问题是 jni 头文件在由 C++ 编译器编译时假定 C++ 代码,因此提供 C++ 样式 API,它与 C 样式 API 不兼容。查看jni.h - 你会在那里找到很多#ifdef __cplusplus
    • @Andreas 如何更改它? “将您的编译器切换到 gcc 以将源代码编译为 C 代码”。请告诉我。我用的是 Eclipse,用的是 Android 项目。
    • 假设您已经创建了一个 C++ 项目并假设您正在使用 Eclipse 的内部构建器(意味着不是基于 Makefile 的项目),将您的 .cpp 文件重命名为 .c 就足够了
    【解决方案2】:

    我正在解决其他 SO 帖子上的工作。 我直接用了这个——” gcc -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 -L /usr/lib/jvm/java-6-sun-1.6.0.26/jre/lib/i386/server -ljvm CallJVM.c 并创建了 a.out。然后我需要将它与服务器文件夹中的 libjvm.so 链接,如帖子中所述。

    很漂亮的解释已经放上here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多