Use JNI twice : Unable to create JVM

I want to use a java method to call an C++ method, and in that C++ method another java method is called. Then i use two types of JNI together and found this problem.

If i use the java method to call the C++ method, or use the C++ method to call the other java method , it is alright.

--

This is a simple test :

I use the native method "call()" in "Mosquito.java" to call method "Java_Mosquito_call" in main.cpp. In that method i use "myWindow()" to call the main method in WindowDemo.java.

Mosquito.java :

publicclass Mosquito{

publicnativevoid call();

static{

System.loadLibrary("mosquito");

}

publicstaticvoid main(String argsp[]){

new Mosquito().call();

}

}

main.cpp :

#include <iostream.h>

#include <string.h>

#include"Frog.h"

#include"Mosquito.h"

JNIEXPORTvoid JNICALL Java_Mosquito_call

(JNIEnv *env, jobject obj)

{

Frog f;

f.prt();

f.myWindow();

}

Frog.h :

#include <iostream.h>

#include <string.h>

#include <jni.h>

class Frog{

public :

void prt()

{

cout <<"This is class Frog !\n" << endl;

}

public :

void myWindow()

{

JNIEnv *env;

JavaVM *jvm;

JavaVMInitArgs vm_args;

JavaVMOption options[1];

jint java_error;

jclass java_class;

jmethodID java_method;

jstring jstr;

jobjectArray args;

options[0].optionString ="-Djava.class.path=.";

options[0].extraInfo = NULL;

memset (&vm_args,0,sizeof(vm_args));

vm_args.version = JNI_VERSION_1_4;

vm_args.nOptions = 1;

vm_args.options = options;

java_error = JNI_CreateJavaVM(&jvm, (void **) &env, &vm_args);

if(java_error < 0)

{

cout <<"ERROR: Unable to create JVM\n" << endl;

}

java_class = env->FindClass("WindowDemo");

if(java_class == NULL)

{

cout <<"ERROR: Unable to find class 'WindowDemo'\n" << endl;

}

java_method = env -> GetStaticMethodID(java_class,"main","([Ljava/lang/String;)V");

if (java_method == NULL)

{

cout <<"error finding 'main' method\n" << endl;

}

env -> CallStaticVoidMethod(java_class,java_method,NULL);

jvm -> DestroyJavaVM();

}

};

I also test to use "JNI_CreateJavaVM()" twice in one C++ file.And the seccond will failed.

[4041 byte] By [Taurenla] at [2007-10-3 1:22:28]
# 1

No need to re-create the JVM:Frog.cpp :

#include <iostream.h>

#include <string.h>

#include "Frog.h"

#include "Mosquito.h"

JNIEXPORT void JNICALL Java_Mosquito_call(JNIEnv * env, jobject obj)

{

Frog f;

f.prt();

f.myWindow(env);

}

Frog.h :

#include <iostream.h>

#include <string.h>

#include <jni.h>

class Frog

{

public :

void prt()

{

cout << "This is class Frog !\n" << endl;

}

void myWindow(JNIEnv * env)

{

jclass java_class = env->FindClass("WindowDemo");

if (java_class == NULL)

{

cout << "ERROR: Unable to find class 'WindowDemo'\n" << endl;

}

jmethodID java_method = env->GetStaticMethodID(java_class, "main", "([Ljava/lang/String;)V");

if (java_method == NULL)

{

cout << "ERROR: Unable to find 'main' method\n" << endl;

}

env->CallStaticVoidMethod(java_class, java_method, NULL);

}

};

Regards

jfbrierea at 2007-7-14 18:19:43 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2
Thank you : )
Taurenla at 2007-7-14 18:19:43 > top of Java-index,Java HotSpot Virtual Machine,Specifications...