JNI and _Jv_Compiler_Properties (+patch)

Martin Kahlert martin.kahlert@infineon.com
Mon Jan 29 06:10:00 GMT 2001


After i got a JNI prog working, where Java called C,
i now try it the other way round. Seems to be more complicate.
My C source is this: (The java part isn't needed here, since
it doesn't even come near it).
cat chello.c:
------------------------------------------------------------------
#include <jni.h>
#include <stdio.h>
#ifdef _WIN32
#define PATH_SEPARATOR ';'
#else /* UNIX */
#define PATH_SEPARATOR ':'
#endif
#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);
}
------------------------------------------------------------------
The code bombs on JNI_CreateJavaVM with a SegFault.
I tried this path:
*** /home/kahlert/LOCAL/BBB/gcc-20010122/libjava/java/lang/natSystem.cc	Fri Jan 12 20:16:05 2001
--- natSystem.cc	Mon Jan 29 14:10:21 2001
*************** java::lang::System::init_properties (voi
*** 432,447 ****
 
 // Set some properties according to whatever was compiled in with
 // `-D'.
! for (int i = 0; _Jv_Compiler_Properties[i]; ++i)
! {
! const char *s, *p;
! // Find the `='.
! for (s = p = _Jv_Compiler_Properties[i]; *s && *s != '='; ++s)
! 	;
! jstring name = JvNewStringLatin1 (p, s - p);
! jstring val = JvNewStringLatin1 (*s == '=' ? s + 1 : s);
! newprops->put (name, val);
! }
 
 // Set the system properties from the user's environment.
 if (_Jv_Environment_Properties)
--- 432,448 ----
 
 // Set some properties according to whatever was compiled in with
 // `-D'.
! if ( _Jv_Compiler_Properties )
! for (int i = 0; _Jv_Compiler_Properties[i]; ++i)
! {
! const char *s, *p;
! // Find the `='.
! for (s = p = _Jv_Compiler_Properties[i]; *s && *s != '='; ++s)
! ;
! jstring name = JvNewStringLatin1 (p, s - p);
! jstring val = JvNewStringLatin1 (*s == '=' ? s + 1 : s);
! newprops->put (name, val);
! }
 
 // Set the system properties from the user's environment.
 if (_Jv_Environment_Properties)
And the SegFault went away.
I grepped for _Jv_Compiler_Properties and found one inside jvgenmain.c,
where it is set to a 0 terminated list of strings (the -Dfoo things).
I assume, the lines
else if (! strncmp (ia->options[i].optionString, "-D", 2))
 {
 // FIXME.
 continue;
 }
in jni.cc (function JNI_CreateJavaVM) are meant to set these Properties, too.
Unfortunately _Jv_Compiler_Properties seems to be a common array
*for all virtual machines*, which i guess is wrong since it could be
different for every particular one.
So shouldn't _Jv_Compiler_Properties be a field in JavaVM?
Then JNI_CreateJavaVM should create this stuff.
(My example doesn't set any -D things, so the list should be only one
entry with 0 inside).
Please tell me, if i am correct.
I cannot run my example, since gnu::gcj::jni::NativeThread bombs with an
exception.
The other thing i found out is, that the types in jni.h are mostly void*
instead for example JNIEnv *. Should i do anything about that?
Thanks,
Martin.
-- 
The early bird gets the worm. If you want something else for 
breakfast, get up later.


More information about the Java mailing list

AltStyle によって変換されたページ (->オリジナル) /