gpt4 book ai didi

java - 在 C 中使用 JNI 未定义 CallXXXMethod

转载 作者:太空宇宙 更新时间:2023-11-04 02:41:53 25 4
gpt4 key购买 nike

所以我尝试使用 JNI 接口(interface)从 C 调用 Java 方法。调用静态方法没有问题,但是当我想调用一个对象的方法时,我就卡住了。代码如下:

#include <stdio.h>
#include <string.h>

#include <jni.h>

int main()
{
JavaVMOption options[1];
JNIEnv *env;
JavaVM *jvm;
JavaVMInitArgs vm_args;
long status;
jclass cls, cls2;
jmethodID mid;
jint square;
jboolean not;
jobject person;

options[0].optionString ="-Djava.class.path=.";
memset(&vm_args, 0, sizeof(vm_args));
vm_args.version = JNI_VERSION_1_8;
vm_args.nOptions = 1;
vm_args.options = options;
status = JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args);

if (status != JNI_ERR) {
cls = (*env)->FindClass(env, "Test");
if (cls != 0) {
mid = (*env)->GetStaticMethodID(env, cls, "square", "(I)I");
if (mid != 0) {
square = (*env)->CallStaticIntMethod(env, cls, mid, 7);
printf("Square: %i\n", square);
}

/*mid = (*env)->GetStaticMethodID(env, cls, "newWindow", "()V");
if (mid) {
(*env)->CallStaticVoidMethod(env, cls, mid, NULL);
}*/

mid = (*env)->GetStaticMethodID(env, cls, "newPerson", "(ILjava/lang/String;)LPerson;");
if (mid) {
person = (*env)->CallStaticObjectMethod(env, cls, mid, 23, "Nox");
cls2 = (*env)->FindClass(env, "Person");
mid = (*env)->GetMethodID(env, cls2, "getAge", "()I");
jint age = (*env)->CallIntMethod(env, cls2, mid, NULL);
printf("age: %i\n", age);
}
}
(*jvm)->DestroyJavaVM(jvm);
return 0;
}

}

我想调用的方法是来自 Person 对象的“getAge”。Person 对象从主类测试方法“newPerson”返回。当我运行该程序时,出现以下错误:

#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x00002b2a136b8e00, pid=12928, tid=47459705508864
#
# JRE version: Java(TM) SE Runtime Environment (8.0_25-b17) (build 1.8.0_25-b17)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.25-b02 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# V [libjvm.so+0x6a9e00] jni_invoke_nonstatic(JNIEnv_*, JavaValue*, _jobject*, JNICallType, _jmethodID*, JNI_ArgumentPusher*, Thread*)+0x1c0
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /home/noxet/pt/c/jni/hs_err_pid12928.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.sun.com/bugreport/crash.jsp
#
Aborted
make: *** [run] Error 134

我的 Java 代码非常简单:

class Person {
private int age;
private String name;

public Person(int age, String name) {
this.age = age;
this.name = name;
}

public int getAge() {
return this.age;
}

public String getName() {
return this.name;
}
}

class Test {
public static int square(int n) {
return n * n;
}

public static boolean not(boolean bool) {
return !bool;
}

public static Person newPerson(int age, String name) {
return new Person(age, name);
}
}

最佳答案

代码中需要进行一些修复:

  1. CallIntMethod 应该是 (*env)->CallIntMethod
  2. class Test 应该是 public
  3. 调用应该是 jint age = (*env)->CallIntMethod(env, mod_obj, mid, NULL);

请注意,调用静态函数需要类名,调用方法需要对象。 (cls2 -> 人)

mid = (*env)->GetStaticMethodID(env, cls, "newPerson", "(ILjava/lang/String;)LPerson;");
mod_obj = mid;
if (mid) {
person = (*env)->CallStaticObjectMethod(env, cls, mid, 23, "Nox");
cls2 = (*env)->FindClass(env, "Person");
mid = (*env)->GetMethodID(env, cls2, "getAge", "()I");
jint age = (*env)->CallIntMethod(env, mid_obj, mid, NULL);
printf("age: %i\n", age);
}

关于java - 在 C 中使用 JNI 未定义 CallXXXMethod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31001165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com