我想使用JNI函數來創建一個Java類並使用DeviceId.java構造函數方法設置該類的某些屬性。我能夠使用GetMethodID獲取構造函數方法,但是如何創建Device.java的新實例,然後設置屬性(setId和setCache)。目標是將完全填充的Device.java對象實例返回給調用者。有任何想法嗎?使用JNI來創建,填充和返回一個Java類實例
JNI功能:
JNIEXPORT jobject JNICALL Java_com_test_getID(JNIEnv *env, jclass cls)
{
jmethodID cnstrctr;
jclass c = (*env)->FindClass(env, "com/test/DeviceId");
if (c == 0) {
printf("Find Class Failed.\n");
}else{
printf("Found class.\n");
}
cnstrctr = (*env)->GetMethodID(env, c, "<init>", "(Ljava/lang/String;[B)V");
if (cnstrctr == 0) {
printf("Find method Failed.\n");
}else {
printf("Found method.\n");
}
return (*env)->NewObject(env, c, cnstrctr);
}
的Java類:
package com.test;
public class DeviceId {
private String id;
private byte[] cache;
public DeviceId(){}
public DeviceId(String id, byte[] cache){
this.id=id;
this.cache=cache;
}
public byte[] getCache() {
return cache;
}
public void setCache(byte[] cache) {
this.cache = cache;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
什麼是''[''中的[B''(Ljava/lang/String; [B] V「'? – 2016-08-30 18:48:19
@WilburWhateley,它是[字節數組](http://stackoverflow.com/a/30815321/4440694) – 2016-10-14 02:08:20