错误:的基操作数��->�� 具有非指针类型��JNIEnv公司��

 2023-01-21    282  

问题描述

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

我在Internet上找到了这个示例,并且在运行它时遇到了一些问题.

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

错误:的基操作数��-&gt;�� 具有非指针类型��JNIEnv公司��

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.H,但仍然为什么缺少参考?
我专门提及我的文件夹在包括路径上.

推荐答案

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

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

要么更改C

之类的变体

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

C++变体,例如

env->FindClass(...)

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

其他推荐答案

我将在其他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链接在服务器文件夹中的libjvm.so,如帖子.

中所述
已经提出了一个非常美丽的解释,在这里/p>

以上所述是小编给大家介绍的错误:的基操作数��->�� 具有非指针类型��JNIEnv公司��,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!

原文链接:https://77isp.com/post/26329.html

=========================================

https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。