Search code examples
c++java-native-interfacejnienv

c++ using JNI: AttachCurrentThread returns -1


I use the JNI in a cpp project, and JNI_GetCreatedJavaVMs is the function how I get JavaVM*.

In the project, there is one cpp function which use JNI. And I when use it in one func, it works well. But when it use in another func, the AttachCurrentThread will return JNI_ERR. The two situation are in different threads, and the JavaVM* is always valid. I don't know why it happens.

I logged some information of the environment. I use GetEnv to get JNIEnv*, return JNI_EDETACHED, so I used AttachCurrentThread. And I'm sure the JavaVM* I used is valid in whole process. Besides, two different situation do not in one thread, and every time when I execute the program, I will meet this problem


Solution

  • I solved the problem. This situation is because of resource limit of thread. I create new thread when call cpp function with JNI. And finally it works.