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.

