This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Patch for my first running JNI invocation program


Hi!

Using this trivial patch (without it local_ref_table
and global_ref_table are NULL and cause a SegFault)

2001-07-19  Martin Kahlert  <martin.kahlert@infineon.com>

	*  jni.cc (JNI_CreateJavaVM): Call _Jv_JNI_Init in order
	to initialize global_ref_table/local_ref_table.

*** gcc.orig/libjava/jni.cc	Sat Jun 16 01:44:45 2001
--- gcc/libjava/jni.cc	Wed Jul 18 15:37:16 2001
***************
*** 2032,2037 ****
--- 2032,2040 ----
  
    the_vm = nvm;
    *vm = the_vm;
+ 
+   _Jv_JNI_Init();
+ 
    return 0;
  }
  
I got the following program, which uses JNI invocation API, 
to work with the current mainline compiler:

cat chello.c

#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);
}


cat hello.java
public class hello
{
 public static void main(String[] args)
    {
     int i = 0;
     System.out.println("Hello World");
     while ( i < args.length)
         System.out.println(args[i++]);
    }
}

gcj -c -o hello.o hello.java
gcc -g -o chello chello.c hello.o -lgcj -lgcjgc -lzgcj -lm
./chello
Hello World
 from C!


Thanks for your great Work!!!
Martin.

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]