简体   繁体   English

两次调用 JNI_CreateJavaVM 函数

[英]Calling JNI_CreateJavaVM function twice

I'm using a library that calls the JNI_CreateJavaVM function inside the library code.我正在使用一个在库代码中调用JNI_CreateJavaVM函数的库。 However, I also need some JNI Wrappings and I need to call the same function JNI_CreateJavaVM to get the JNIEnv* to my application.但是,我还需要一些 JNI Wrappings,我需要调用相同的函数JNI_CreateJavaVM来将JNIEnv*发送到我的应用程序。

But the second call is failing.但是第二次调用失败了。

is there any way to do this?有没有办法做到这一点?

The library does not support getting or setting the JNIEnv* created inside the library.该库不支持获取或设置在库内创建的JNIEnv*

You cannot create more than one JVM from the same process:您不能从同一进程创建多个 JVM:

As of JDK/JRE 1.2 , creation of multiple VMs in a single process is not supported.从 JDK/JRE 1.2 开始,不支持在单个进程中创建多个 VM。

You may be able to attach your current thread to the existing JVM though using AttachCurrentThread function.您可以使用AttachCurrentThread函数将当前线程附加到现有的 JVM。 See the docs for the Invocation API .请参阅调用 API 的文档 The equivalent document in Java 15 simply states: Java 15 中等效文档只是说明:

Creation of multiple VMs in a single process is not supported.不支持在单个进程中创建多个 VM。

You will need a pointer to the JavaVM object.您将需要一个指向JavaVM对象的指针。 See if JNI_GetCreatedJavaVMs() can help you, I'm not sure if this is per-process (in which case it will only ever be a single element list) or per machine.看看JNI_GetCreatedJavaVMs()可以帮助您,我不确定这是每个进程(在这种情况下它只会是单个元素列表)还是每台机器。 In either case the JavaVM will have to be the one that the library is using or you probably will not be doing what you want.在任何一种情况下, JavaVM都必须是库正在使用的那个,否则你可能不会做你想做的事。 If you can access that then you should be able to make invocations on other objects in your Java application, but make sure that it is thread-safe.如果您可以访问它,那么您应该能够对 Java 应用程序中的其他对象进行调用,但要确保它是线程安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM