jnienv

Caching JNI objects and thread-safety (in Android)

自古美人都是妖i 提交于 2019-11-30 23:51:53
I'm writing a C++ app with native threads (pthreads) and I need to call some Java methods etc. I'm not sure which JNI objects can be safely cached ie stored in my C++ object for use later, possibly/probably by a different thread. I do know that if my class' methods can be called by different threads I mustn't cache the JNIEnv, but instead cache the JavaVM and get a JNIEnv by attaching the current thread. But does that also mean I can't cache anything obtained from a JNIEnv? I need to use the objects obtained by the following JNIEnv methods: FindClass, GetMethodID, NewObject, NewGlobalRef Do

Caching JNI objects and thread-safety (in Android)

前提是你 提交于 2019-11-30 18:13:00
问题 I'm writing a C++ app with native threads (pthreads) and I need to call some Java methods etc. I'm not sure which JNI objects can be safely cached ie stored in my C++ object for use later, possibly/probably by a different thread. I do know that if my class' methods can be called by different threads I mustn't cache the JNIEnv, but instead cache the JavaVM and get a JNIEnv by attaching the current thread. But does that also mean I can't cache anything obtained from a JNIEnv? I need to use the

JNI in C++ to read file to jbyteArray

江枫思渺然 提交于 2019-11-30 09:26:32
问题 I am writing a C++ program in UNIX to generate a shared library which will be called in java using JNI. This C++ program has to read a file in UNIX box then it will have to be converted into jbyteArray (JNI data type) so that JAVA can use it. I read the file in C++ into char* but could not convert into jbyteArray . Please help it. Code is below:: #include <iostream> #include <fstream> #include <stdio.h> #include "com_sp_dll_NativeMethods.h" // this header file was generated by java using

JNI_CreateJavaVM() terminates with exit code 1

三世轮回 提交于 2019-11-29 13:46:06
I'm trying to call a Java method from C++ using JNI. To do that I've installed jdk1.7.0_51 , linking against jdk1.7.0_51\lib\jvm.lib , including jdk1.7.0_51\include and jdk1.7.0_51\include\win32 . using the following code in Visual Studio 2012 I tried to create a Java vm object - but the function always terminates my application with exit code 1 (the function doesn't return 1: my program terminates completly and sends the exit code 1). #include <iostream> #include "jni.h" int main(int argc, char*argv[]){ JNIEnv* env = nullptr; JavaVM* jvm = nullptr; JavaVMInitArgs vm_args; JavaVMOption options

Returning local reference created by JNI from a native method

大憨熊 提交于 2019-11-29 12:45:37
JNI reference says that "Local references are valid for the duration of a native method call. They are freed automatically after the native method returns. Source: http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/functions.html#global_local I'm kind of lost here. According to above, I must explicitly call NewGlobalRef and pass object returned from a call to NewObject . I tried this and it seems that when a GC kicks in, it does not collect my references (like something still holds onto them). Consider the following project: Main.java : package lv.example; import java.io

Returning local reference created by JNI from a native method

爱⌒轻易说出口 提交于 2019-11-28 06:37:41
问题 JNI reference says that "Local references are valid for the duration of a native method call. They are freed automatically after the native method returns. Source: http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/functions.html#global_local I'm kind of lost here. According to above, I must explicitly call NewGlobalRef and pass object returned from a call to NewObject . I tried this and it seems that when a GC kicks in, it does not collect my references (like something still

what is wrong with this call to the java method?

给你一囗甜甜゛ 提交于 2019-11-28 01:05:48
问题 I am trying to call a Java method from the code. C code listens to either Escape , Shift , Ctrl key press, then it calls the Java method telling which key was pressed. Following are the snippets that play a role in this. C Snippet: 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

Keeping a global reference to the JNIEnv environment

折月煮酒 提交于 2019-11-27 06:44:41
I am storing off JNIEnv in a global so I can call static java methods later. But is it nessasary to store off a global pointer to the JNIEnv , they way one would with any other java object, or is it a special case that does not require this. JNIEnv* globalEnvPointer; [JNICALL etc] void init(JNIENv* env, [etc]) { //required? globalEnvPointer = (JNIENv*) (env*)->GetGlobalRef(env, env); //or is this OK? globalEnvPointer = env; } Edit I'm bing a bit dumb here, all the methods that will use globalEnvPointer , are invoked within my init because my init is actually my c program's main method, which