【发布时间】:2015-09-01 21:40:46
【问题描述】:
下面是我的 C 和 Java 代码。 Java 调用function1 来收集String 和integer 并使用ResultCollector 对象返回它们。 ResultCollector 是一个外部类,换句话说,它没有嵌套在ResultCollecter 类中。此外,它具有三个构造函数。我还有其他函数可以使用带有(IF)V 签名的构造函数。此外,第三个构造函数也不能正常工作(即(II)V)。
Java 代码是:
package user.directory;
public class ResultCollector {
private int err;
private float resVal;
private String resID;
/**
* Signature (IF)V
*/
public ResultCollector(int err, float value) {
this.err = err;
this.resVal = value;
}
/**
* Signature (II)V
*/
public ResultCollector(int err, int value) {
this.err = err;
this.resVal = (float) value;
}
/**
* Signature (ILjava/lang/;)V
*/
public ResultCollector(int err, char[] id) {
this.err = err;
this.resID = String.copyValueOf(id);
}
public String id() {
return resID;
}
public int err() {
return err;
}
public float value() {
return resVal;
}
public void setParam(int err, String id, float value) {
this.err = err;
this.resID = id;
this.resVal = value;
}
}
而C代码是:
JNIEXPORT jobject JNICALL Java_project_function1(
JNIEnv *env, jobject obj, jint index) {
jint t;
char *id = C function to return string;
t = an error that is needed;
if (c == NULL)
// throw exception
return NULL;
jstring name = (*env)->NewStringUTF(env, id);
if (name == NULL)
// throw exception
return NULL;
jclass c = (*env)->FindClass(env,
"user/directory/ResultCollector");
jmethodID constr = (*env)->GetMethodID(env, c, "<init>", "(ILjava/lang/String;)V");
if (constr == NULL)
//cannot get the constructor correctly
return NULL;
jobject result = (*env)->NewObject(env, c, constr, t, name);
return result;
}
我的问题是:如何初始化外部类?这段代码哪里出错了?
【问题讨论】:
标签: string object constructor java-native-interface