从JNI方法中创建JVM

时间:2022-05-18 17:20:01

Is it possible to create a JVM from within a JNI method using the JNI API?

是否可以使用JNI API在JNI方法中创建JVM?

I've tried to do this using the JNI function "JNI_CreateJavaVM()", but it's not working (the function keeps returning a value less than zero).

我试图使用JNI函数“JNI_CreateJavaVM()”来做这件事,但它不起作用(该函数保持返回小于零的值)。

Here is the basic code I'm using (C++):

这是我正在使用的基本代码(C ++):

JNIEnv *env;
JavaVM *jvm;
jint res;

#ifdef JNI_VERSION_1_2
JavaVMInitArgs vm_args;
JavaVMOption options[2];
options[0].optionString =
    "-Djava.class.path=" USER_CLASSPATH;
options[1].optionString = "-verbose:jni";
vm_args.version = JNI_VERSION_1_2;
vm_args.options = options;
vm_args.nOptions = 2;
vm_args.ignoreUnrecognized = JNI_TRUE;
/* Create the Java VM */
res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);

#else

JDK1_1InitArgs vm_args;
char classpath[1024];
vm_args.version = 0x00010001;
JNI_GetDefaultJavaVMInitArgs(&vm_args);
/* Append USER_CLASSPATH to the default system class path */
sprintf(classpath, "%s%c%s",
        vm_args.classpath, PATH_SEPARATOR, USER_CLASSPATH);
vm_args.classpath = classpath;
/* Create the Java VM */
res = JNI_CreateJavaVM(&jvm, &env, &vm_args);

#endif /* JNI_VERSION_1_2 */

Where USER_CLASSPATH contains the path to the classes I want to load. After the above code executes, res < 0, indicating that JNI_CreateJavaVM() failed. The code above is part of a native method written in C++ called from Java. Any ideas on how to get this to work?

USER_CLASSPATH包含我要加载的类的路径。执行上述代码后,res <0,表示JNI_CreateJavaVM()失败。上面的代码是用Java编写的用C ++编写的本机方法的一部分。有关如何使其工作的任何想法?

Thanks.

2 个解决方案

#1


6  

No, you can't. It's a documented restriction that you can only have one JVM at a time. The API is designed for the possibility of extension, but the extension has never happened.

不,你不能。这是一个记录在案的限制,一次只能有一个JVM。 API是为扩展的可能性而设计的,但扩展从未发生过。

If you are in a JNI method, then there is already one JVM, and one JVM per process is all you get.

如果您使用的是JNI方法,那么就已经有一个JVM,每个进程都有一个JVM。

#2


1  

I see what you mean: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4479303

我明白你的意思了:http://bugs.sun.com/bugdatabase/view_bug.do?video_id = 4479303

The bug report says it's not possible to run multiple JVMs in the same address space. I have to say I'm a little surprised that JNI_CreateJavaVM() doesn't fork off a new JVM in a different address space.

错误报告说不可能在同一地址空间中运行多个JVM。我不得不说,我有点惊讶JNI_CreateJavaVM()不会在不同的地址空间中分叉新的JVM。

Since JNI_CreateJavaVM() doesn't fork a new process itself, is it possible to manually fork off another JVM process from within a JNI method and subsequently use IPC to manage it? If so, what's the best way to do this? A literal fork()/exec() doesn't seem like a good idea because it would copy the entire (probably very large) address space of the JVM only to throw it away immediately afterward.

由于JNI_CreateJavaVM()本身不分叉新进程,是否可以从JNI方法中手动分离另一个JVM进程,然后使用IPC来管理它?如果是这样,最好的方法是什么?文字fork()/ exec()似乎不是一个好主意,因为它会复制JVM的整个(可能是非常大的)地址空间,只是在之后立即丢弃它。

#1


6  

No, you can't. It's a documented restriction that you can only have one JVM at a time. The API is designed for the possibility of extension, but the extension has never happened.

不,你不能。这是一个记录在案的限制,一次只能有一个JVM。 API是为扩展的可能性而设计的,但扩展从未发生过。

If you are in a JNI method, then there is already one JVM, and one JVM per process is all you get.

如果您使用的是JNI方法,那么就已经有一个JVM,每个进程都有一个JVM。

#2


1  

I see what you mean: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4479303

我明白你的意思了:http://bugs.sun.com/bugdatabase/view_bug.do?video_id = 4479303

The bug report says it's not possible to run multiple JVMs in the same address space. I have to say I'm a little surprised that JNI_CreateJavaVM() doesn't fork off a new JVM in a different address space.

错误报告说不可能在同一地址空间中运行多个JVM。我不得不说,我有点惊讶JNI_CreateJavaVM()不会在不同的地址空间中分叉新的JVM。

Since JNI_CreateJavaVM() doesn't fork a new process itself, is it possible to manually fork off another JVM process from within a JNI method and subsequently use IPC to manage it? If so, what's the best way to do this? A literal fork()/exec() doesn't seem like a good idea because it would copy the entire (probably very large) address space of the JVM only to throw it away immediately afterward.

由于JNI_CreateJavaVM()本身不分叉新进程,是否可以从JNI方法中手动分离另一个JVM进程,然后使用IPC来管理它?如果是这样,最好的方法是什么?文字fork()/ exec()似乎不是一个好主意,因为它会复制JVM的整个(可能是非常大的)地址空间,只是在之后立即丢弃它。