Java对象构造函数的GetMethodID
我有一个与 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
thisObject
是一个ExampleJNI
而不是Order
,因此GetObjectClass
将返回ExampleJNI
,但它不会没有您正在寻找的构造函数。将GetObjectClass
更改为env->FindClass("Order")
。thisObject
is aExampleJNI
not aOrder
soGetObjectClass
will returnExampleJNI
which doesn't have the constructor you are looking for. ChangeGetObjectClass
toenv->FindClass("Order")
.