How far is the JNI invocation API?

Martin Kahlert martin.kahlert@infineon.com
Mon Apr 23 01:43:00 GMT 2001


Hi!
Some time ago i tried to get the invocation API of JNI to work
and managed fix some segmentation faults,
but somebody told me, that there was no support for this in gcj up to now.

How far is the invocation API implemented, now?

I want to start a JVM from C using JNI like this:

#include <jni.h>
#include <stdio.h>

#define PATH_SEPARATOR ':'

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

int main()
{
 JNIEnv *env;
 JavaVM *jvm;
 JavaVMInitArgs vm_args;
 jint res;
 jclass cls;
 jmethodID mid;
 jstring jstr;
 jobjectArray args;
 char classpath[1024];

 vm_args.version = JNI_VERSION_1_2;

 JNI_GetDefaultJavaVMInitArgs(&vm_args);


 /* Create the Java VM */
 res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
 if (res < 0)
    {
     fprintf(stderr, "Can't create Java VM\n");
     exit(1);
    }

 cls = (*env)->FindClass(env, "hello");
 if (cls == 0) {
     fprintf(stderr, "Can't find hello class\n");
     exit(1);
 }

 mid = (*env)->GetStaticMethodID(env, cls, "main", "([Ljava/lang/String;)V");
 if (mid == 0) {
     fprintf(stderr, "Can't find Prog.main\n");
     exit(1);
 }

 jstr = (*env)->NewStringUTF(env, " from C!");
 if (jstr == 0) {
     fprintf(stderr, "Out of memory\n");
     exit(1);
 }
 args = (*env)->NewObjectArray(env, 1, 
         (*env)->FindClass(env, "java/lang/String"), jstr);
 if (args == 0) {
     fprintf(stderr, "Out of memory\n");
     exit(1);
 }
 (*env)->CallStaticVoidMethod(env, cls, mid, args);

 (*jvm)->DestroyJavaVM(jvm);
}


If this is not possible, i would try another approach by providing a dummy
main routine in java, which calls my C main program using JNI.
This way, a working JVM should be present in the prozess and my prog
could work or do i miss anything important?

Thanks for every clarification in advance,
Martin.

-- 
The early bird gets the worm. If you want something else for       
breakfast, get up later.



More information about the Java mailing list