Cant Create JVM through JNI_CreateJavaVM()
1 posts in topic
Flat View  Flat View

Posted By:   Ritu_Gupta
Posted On:   Tuesday, September 27, 2005 08:59 AM

i am not able to create the JVM from my C++ code using JNI_CreateJavaVM() method.the &jvm,&enu arguments of this methods are null.i have tried almost do i handle this.let me know asap.



Re: Cant Create JVM through JNI_CreateJavaVM()

Posted By:   shiladitya_sircar  
Posted On:   Wednesday, September 28, 2005 12:29 PM

Since you have not provided the environment you are trying to achieve this, I am assuming its on Win32 platform. The sample code below explains how to achieve this:

On C++ side of things:

JNIEnv* create_vm() {
JavaVM* jvm;
JNIEnv* env;
JavaVMInitArgs args;
JavaVMOption options[1];

args.version = JNI_VERSION_1_4; // JNI_VERSION_1_2 is interchangeable for this example
args.nOptions = 1;
options[0].optionString = "-Djava.class.path=c:\projects\local\inonit\classes";
args.options = options;
args.ignoreUnrecognized = JNI_FALSE;
JNI_CreateJavaVM(&jvm, (void **)&env, &args);
return env;

void invoke_class(JNIEnv* env) {
jclass helloWorldClass;
jmethodID mainMethod;
jobjectArray applicationArgs;
jstring applicationArg0;

helloWorldClass = (*env)->FindClass(env, "example/jni/InvocationHelloWorld");

mainMethod = (*env)->GetStaticMethodID(env, helloWorldClass, "main", "([Ljava/lang/String;)V");

applicationArgs = (*env)->NewObjectArray(env, 1, (*env)->FindClass(env, "java/lang/String"), NULL);
applicationArg0 = (*env)->NewStringUTF(env, "From-C-program");
(*env)->SetObjectArrayElement(env, applicationArgs, 0, applicationArg0);

(*env)->CallStaticVoidMethod(env, helloWorldClass, mainMethod, applicationArgs);

int main(int argc, char **argv) {
JNIEnv* env = create_vm();
invoke_class( env );

Java Side of things

package example.jni;

public class InvocationHelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
System.out.println("Arguments sent to this program:");
if (args.length == 0) {
} else {
for (int i=0; i System.out.print(args[i] + " ");

About | Sitemap | Contact