creating a JVM from within a JNI method
Asked Answered
P

2

7

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

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).

Here is the basic code I'm using (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?

Thanks.

Prosopopoeia answered 14/2, 2010 at 2:38 Comment(1)
I was going to ask SO if it's possible to have JNIEXPORT functions in a C++ application which invokes a JVM, and I think this question answers mine (no).Tletski
B
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.

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

Beetlebrowed answered 14/2, 2010 at 3:22 Comment(3)
Why do you say he has many JVM at a time. There is a #if. He starts only one depending on the JDK versionCatalinacatalo
If he's already in a JNI method then he has one JVM and is trying to create a second.Beetlebrowed
OK, I skipped the fact that he was calling C++ from Java, I though the main process was C++, that's when you use JNI_CreateJavaVM()Catalinacatalo
P
1

I see what you mean: https://bugs.java.com/bugdatabase/view_bug?bug_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.

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.

Prosopopoeia answered 14/2, 2010 at 4:19 Comment(1)
Interesting. I kind of want to try this out just for the heck of it. :)Sirrah

© 2022 - 2024 — McMap. All rights reserved.