两次调用JNI_CreateJavaVM函数 [英] Calling JNI_CreateJavaVM function twice

查看:250
本文介绍了两次调用JNI_CreateJavaVM函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个在库代码中调用JNI_CreateJavaVM函数的库.但是,我还需要一些JNI包装,并且需要调用相同的函数JNI_CreateJavaVMJNIEnv*移至我的应用程序.

I'm using a library that calls the JNI_CreateJavaVM function inside the library code. However, I also need some JNI Wrappings and I need to call the same function JNI_CreateJavaVM to get the JNIEnv* to my application.

但是第二个呼叫失败.

有什么办法吗?

该库不支持获取或设置在库内创建的JNIEnv*.

The library does not support getting or setting the JNIEnv* created inside the library.

推荐答案

您不能在同一进程中创建多个JVM:

You cannot create more than one JVM from the same process:

从JDK/JRE 1.2开始,不支持在单个进程中创建多个VM.

As of JDK/JRE 1.2 , creation of multiple VMs in a single process is not supported.

您可以使用AttachCurrentThread函数将当前线程附加到现有的JVM.请参见有关Invocation API的文档. Java 15中的等效文档简单声明:

You may be able to attach your current thread to the existing JVM though using AttachCurrentThread function. See the docs for the Invocation API. The equivalent document in Java 15 simply states:

不支持在单个进程中创建多个VM.

Creation of multiple VMs in a single process is not supported.

您将需要一个指向JavaVM对象的指针.看看JNI_GetCreatedJavaVMs()是否可以帮助您,我不确定这是按进程(在这种情况下,它永远只是单个元素列表)还是按机器.在任何一种情况下,JavaVM都必须是库正在使用的那个,否则您可能不会做您想做的事情.如果可以访问它,那么您应该能够对Java应用程序中的其他对象进行调用,但是请确保它是线程安全的.

You will need a pointer to the JavaVM object. 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. 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. 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.

这篇关于两次调用JNI_CreateJavaVM函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆