错误:' - >'的基本操作数具有非指针类型'JNIEnv' [英] error: base operand of ‘->’ has non-pointer type ‘JNIEnv’

查看:156
本文介绍了错误:' - >'的基本操作数具有非指针类型'JNIEnv'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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

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

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 :

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 ++中实现的不同方式,但我想我正确地写了。

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

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

On compiling with gcc ,I am getting

undefined reference to `JNI_CreateJavaVM'

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

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

推荐答案


我注意到了不同的方式用C和C ++实现,但我想我写得正确。

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

你使用的是 C variant,但正在使用 g ++ 进行编译,它会调用 C ++ 编译器(即使你的源代码文件有 .c 扩展名。)

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

更改 C 变体如

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

C ++ 变体,例如

env->FindClass(...)

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

or switch your compiler to gcc to compile the source as C code.

这篇关于错误:' - &gt;'的基本操作数具有非指针类型'JNIEnv'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆