Runtime error Calling Java Methods from C++

I'm trying to add Java methods to an existing C++ program.

I'm working in W2000 enviroment using Cygnus GCC compiler.

Before adding these methods, I tested invoke example from Jni tutorial.

All worked properly. But the problems arouse when I tried to run non-static methods.

Here is the code I'm testing now:

#include <stdio.h>

#include <jni.h>

#define PATH_SEPARATOR ';'

#define USER_CLASSPATH "." /* where Prog.class is */

int main(int argc, char *argv[]) {

JNIEnv *env;

JavaVM *jvm;

JDK1_1InitArgs vm_args;

jint res;

jclass cls;

jmethodID mid;

jobject jobj;

char classpath[1024];

vm_args.version = 0x00010001;

JNI_GetDefaultJavaVMInitArgs(&vm_args);

sprintf(classpath, "%s%c%s",

vm_args.classpath, PATH_SEPARATOR, USER_CLASSPATH);

vm_args.classpath = classpath;

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

if (res < 0) {

fprintf(stderr, "Can't create Java VM\n");

exit(1);

}

cls = env->FindClass("Prog");

if (cls == 0) {

fprintf(stderr, "Can't find consola class\n");

exit(1);

}

mid = env->GetStaticMethodID(cls, "Entrada", "()V");

if (mid == 0) {

fprintf(stderr, "Can't find consola.consola\n");

exit(1);

}

env->CallStaticVoidMethod(cls, mid);

/*jobj = env->NewObject(cls, mid);

if (jobj == 0) {

fprintf(stderr, "Can't find Prog.main\n");

exit(4);

}

*/

jvm->DestroyJavaVM();

}

And the java code Prog.java:

public class Prog {

public static void Entrada() {

Consola Cons = new Consola();

}

}

And Consola a user class tha shows a Java window and let's do something more.

These code works fine and The Prog and Consola classes run properly.

But when I try to change Entrada method to non-static, following error appears in a window.

Microsoft Visual C++ Runtime library

--

Runtime Error !

Program: D:\javaini\JNI-INVOKE\invoke.exe

abnormal program termination

-

If I try

jobj = env->NewObject(cls, mid);

if (jobj == 0) {

fprintf(stderr, "Can't find Prog.main\n");

exit(4);

}

instead of

env->CallVoidMethod(cls, mid);

I get the same error.

Could anybody help me ?

Am I doing anything wrong ?

Thanks in advance. Ignasi Villagrasa.

[2666 byte] By [NACHOV] at [2007-9-26 13:16:09]
# 1

I found it.

In JNI examples I saw

mid = env->GetMethodID(cls, "<init>", "()V");

to run constructor.

And then NewObject function.

So it's solved.

But I find another problem now.

If the class I'm calling is a graphic java class (In this case from swing), when the window is closed the application in C++ stops too.

Should I create another thread to manage the window ?

Couldn't I follow running lines after NewObject method ?

Thanks in advance, Ignasi Villagrasa.

NACHOV at 2007-7-2 13:24:42 > top of Java-index,Java HotSpot Virtual Machine,Specifications...