Java对象构造函数的GetMethodID

发布于 2025-01-11 07:48:54 字数 2096 浏览 0 评论 0原文

我有一个与 C++ 的 GetMethodID() 函数相关的简短问题。我一直在 StackOverflow 上寻找答案,但找不到。我的主要代码是用 Java 编写的,但对于某些部分我需要 C++。下面的代码是我打算实现的简化,用于测试如何在 Java 和 C++ 之间检索和传递对象。最后一期在本文末尾提出。首先,我介绍一下实现方式。

public class ExampleJNI {

static {
    System.loadLibrary("nativeObject");
}

public static void main(String[] args){
     ExampleJNI tmp  = new ExampleJNI();
     Order o = tmp.createOrder(1,2,3,4);

     if (o != null){
         System.out.println(o.toString());
     } else {
         System.out.println("Errors present");
     }
}

// Try passing a list
public native Order createOrder(int a, int b, int c, int d);

}

订单类定义为:

public class Order {

// Order attributes
public final int a;
public final int b;
public final int c;
public final int d;

private int e;

public Order(int a, int b, int c, int d){
    this.a = a;
    this.b = b;
    this.c = c;
    this.d = d;
}

@Override
public String toString(){
    return a+","+b+","+c+","+d;
}
}

我在 C++ 中有以下实现。

#include "ExampleJNI.h"
#include <iostream>
#include <vector>
/*
 * Class:     ExampleJNI
 * Method:    createOrder
 * Signature: (IIII)LOrder;
 */
JNIEXPORT jobject JNICALL Java_ExampleJNI_createOrder(JNIEnv* env, jobject thisObject,     jint a, jint b, jint c, jint d){

// Get a class reference for Order
jclass cls = env->GetObjectClass(thisObject);

if (cls == NULL){
    std::cout << "Class not found" << std::endl;
    return NULL;
}

// Get the method ID of the constructor whick takes four integers as input
jmethodID cid = env->GetMethodID(cls, "<init>", "(IIII)V");

if (cid == NULL){
    std::cout << "Method not found" << std::endl;
    return NULL;
}

return env->NewObject(cls, cid, a, b, c, d);
}

当我运行代码时,打印“找不到方法”,这表明调用 GetMethodID 时出现问题。我还检索到以下异常错误:

Exception in thread "main" java.lang.NoSuchMethodError: <init>
    at ExampleJNI.createOrder(Native Method)
    at ExampleJNI.main(ExampleJNI.java:11)

非常感谢任何有关我应该使用什么而不是 的建议!

I have a short question related to the GetMethodID() function of C++. I have been searching for the answer here on StackOverflow, but could not find it. My main code is in Java, but for some parts I would need C++. The code below is a simplification of what I intend to implement, to test out how to retrieve and pass objects between Java and C++. The final issue is presented at the end of this pos. First, I present the implementation.

public class ExampleJNI {

static {
    System.loadLibrary("nativeObject");
}

public static void main(String[] args){
     ExampleJNI tmp  = new ExampleJNI();
     Order o = tmp.createOrder(1,2,3,4);

     if (o != null){
         System.out.println(o.toString());
     } else {
         System.out.println("Errors present");
     }
}

// Try passing a list
public native Order createOrder(int a, int b, int c, int d);

}

The order class is defined to be:

public class Order {

// Order attributes
public final int a;
public final int b;
public final int c;
public final int d;

private int e;

public Order(int a, int b, int c, int d){
    this.a = a;
    this.b = b;
    this.c = c;
    this.d = d;
}

@Override
public String toString(){
    return a+","+b+","+c+","+d;
}
}

I have the following implementation in C++.

#include "ExampleJNI.h"
#include <iostream>
#include <vector>
/*
 * Class:     ExampleJNI
 * Method:    createOrder
 * Signature: (IIII)LOrder;
 */
JNIEXPORT jobject JNICALL Java_ExampleJNI_createOrder(JNIEnv* env, jobject thisObject,     jint a, jint b, jint c, jint d){

// Get a class reference for Order
jclass cls = env->GetObjectClass(thisObject);

if (cls == NULL){
    std::cout << "Class not found" << std::endl;
    return NULL;
}

// Get the method ID of the constructor whick takes four integers as input
jmethodID cid = env->GetMethodID(cls, "<init>", "(IIII)V");

if (cid == NULL){
    std::cout << "Method not found" << std::endl;
    return NULL;
}

return env->NewObject(cls, cid, a, b, c, d);
}

When I run the code, "Method not found" is printed, which indicates that something is going wrong when calling the GetMethodID. I also retrieve the following exception error:

Exception in thread "main" java.lang.NoSuchMethodError: <init>
    at ExampleJNI.createOrder(Native Method)
    at ExampleJNI.main(ExampleJNI.java:11)

Any suggestions on what I should have instead of the <init> are highly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

提赋 2025-01-18 07:48:54

thisObject 是一个 ExampleJNI 而不是 Order,因此 GetObjectClass 将返回 ExampleJNI,但它不会没有您正在寻找的构造函数。将 GetObjectClass 更改为 env->FindClass("Order")

thisObject is a ExampleJNI not a Order so GetObjectClass will return ExampleJNI which doesn't have the constructor you are looking for. Change GetObjectClass to env->FindClass("Order").

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文