Tag: jnienv

Monodroid JNI for Javareflection调用私有方法

在Monodroid项目中,我需要能够在类上调用私有方法。 从相关问题的答案来看,似乎这可以通过反思在Java中实现: import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import android.os.ParcelFileDescriptor; … ParcelFileDescriptor pipe[] = null; try { Method createPipeMethod = ParcelFileDescriptor.class.getDeclaredMethod(“createPipe”); pipe = (ParcelFileDescriptor[]) createPipeMethod.invoke(null); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } 我需要使用Monodroid的代码。 不幸的是, java.lang.reflect在Monodroid中不可用 。 但是,有人建议我可以使用Monodroid项目中的JNI运行此代码。 Xamarin文档声明内联JNI是可能的 ,而不必绑定整个JAR。 […]

用C ++编写的JNI将文件读取到jbyteArray

我正在UNIX中编写一个C ++程序来生成一个共享库,该库将使用JNI在java中调用。 这个C ++程序必须在UNIX框中读取文件,然后必须将其转换为jbyteArray (JNI数据类型),以便JAVA可以使用它。 我用C ++将文件读入char*但无法转换为jbyteArray 。 请帮忙。 代码如下:: #include #include #include #include “com_sp_dll_NativeMethods.h” // this header file was generated by java using namespace std; JNIEXPORT void JNICALL Java_HelloWorld_displayMessage(JNIEnv *env, jobject obj) { printf(“Hello World!\n”); } JNIEXPORT jbyteArray JNICALL Java_com_sp_dll_NativeMethods_getFile(JNIEnv *env, jobject obj) { ifstream fl(“/home/rkannan/myFile.txt”); fl.seekg(0, ios::end ); size_t len = fl.tellg(); char […]

这个java方法调用有什么问题?

我试图从代码中调用Java方法。 C代码监听Escape , Shift , Ctrl键,然后调用Java方法告诉按下了哪个键。 以下是在此中发挥作用的片段。 C片段: mid = (*env)->GetMethodID(env,cls,”callBack”,”(Ljava/lang/String;)V”); Env = env; if(called) switch(param) { case VK_CONTROL: printf(“Control pressed !\n”); (*Env)->CallVoidMethodA(Env,Obj,mid,”11″); // calling the java method break; case VK_SHIFT: printf(“Shift pressed !\n”); (*Env)->CallVoidMethodA(Env,Obj,mid,”10″); // calling the java method break; case VK_ESCAPE: printf(“Escape pressed !\n”); (*Env)->CallVoidMethodA(Env,Obj,mid,”1B”); // calling the java method break; default: printf(“The […]