java 错误:“->”的基操作数具有非指针类型“JNIEnv”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15764948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:47:39  来源:igfitidea点击:

error: base operand of ‘->’ has non-pointer type ‘JNIEnv’

javajava-native-interface

提问by Rorschach

#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.

I found this example on internet and I am facing some problems while running it.

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

I am properly specify the build commands as far as I know as,

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

  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

My intention is to actually run it using jikesrvm but to experiment this I chose to work with JVM. The error I am getting is :

我的意图是实际使用 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'

I noticed the different ways of implementing in C and C++ but I think I am writing it correctly.

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

Edit: On compiling with gcc ,I am getting

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

undefined reference to `JNI_CreateJavaVM'

and it is what was being prompted in eclipse but I thought my configurations are not apt.When I use ctrl+clickit takes me to referenced jni.h as well but still why it is missing reference ? am specifically mentioning my folders on include path.

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

回答by Andreas Fester

I noticed the different ways of implementing in C and C++ but I think I am writing it correctly.

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

You are using the Cvariant, but are compiling with g++, which invokes the C++compiler (even if your source file has a .cextension).

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

Either change the Cvariants like

要么更改变C体,如

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

to the C++variants, like

C++变体,比如

env->FindClass(...)

or switch your compiler to gccto compile the source as Ccode.

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

回答by Rorschach

I am putting the work around found on other SO posts.. I used straight forwardly this - " 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.cand a.out was created. Then I need to link it with libjvm.so present in server folder as mentioned in post.

我正在解决在其他 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 链接,如帖子中所述。

A very beautiful explanation has been put up here

一个非常漂亮的解释已经放在这里