[kaffe] doubt in JNI
kabileshkumar cheetancheri
cgkabilesh at yahoo.com
Fri Dec 10 13:07:33 PST 2004
Skipped content of type multipart/alternative-------------- next part --------------
#include <jni.h>
#define PATH_SEPARATOR ';' /* define it to be ':' on Solaris */
#define USER_CLASSPATH "." /* where Prog.class is */
main() {
// JNIEnv env1;
JNIEnv *env;
JavaVM *jvm;
jint res;
jclass cls;
jmethodID mid;
jstring jstr;
jclass stringClass;
jobjectArray args;
#ifdef JNI_VERSION_1_2
JavaVMInitArgs vm_args;
JavaVMOption options[1];
options[0].optionString =
"-Djava.class.path=" USER_CLASSPATH;
vm_args.version = 0x00010002;
vm_args.options = options;
vm_args.nOptions = 1;
vm_args.ignoreUnrecognized = JNI_TRUE;
res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
printf("JNI_VERSION_1_2 \n");
#else
JDK1_1InitArgs vm_args;
char classpath[1024];
vm_args.version = 0x00010001;
JNI_GetDefaultJavaVMInitArgs(&vm_args);
/* Append USER_CLASSPATH to the default system class path */
sprintf(classpath, "%s%c%s",
vm_args.classpath, PATH_SEPARATOR, USER_CLASSPATH);
vm_args.classpath = classpath;
/* Create the Java VM */
res = JNI_CreateJavaVM(&jvm, &env, &vm_args);
printf("JNI_VERSION_1_1 \n");
#endif /* JNI_VERSION_1_2 */
printf("size of JNIEnv = %d\n", sizeof(struct JNINativeInterface_));
//printf("env in demos:%p\n", *env);
//printf("env.FindClass in demos:%p\n", (*env)->FindClass);
if (res < 0) {
fprintf(stderr, "Can't create Java VM\n");
exit(1);
}
if(env == NULL)
{
printf("env == NULL %p\n", *env);
}
else
{
// printf("env == %p\n", *env);
}
cls = (*env)->FindClass(env, "Prog");
//printf("cls = %p\n ",cls);
if (cls == NULL) {
printf("findd class NULL \n");
goto destroy;
}
mid = (*env)->GetStaticMethodID(env, cls, "main","([Ljava/lang/String;)V");
if (mid == NULL) {
goto destroy;
}
jstr = (*env)->NewStringUTF(env, " \n from C!");
printf("\n from c just for fun\n");
if (jstr == NULL) {
goto destroy;
}
stringClass = (*env)->FindClass(env, "java/lang/String");
args = (*env)->NewObjectArray(env, 1, stringClass, jstr);
if (args == NULL) {
goto destroy;
}
(*env)->CallStaticVoidMethod(env, cls, mid, args);
destroy:
printf("destroy\n");
//if ((*env)->ExceptionOccurred(env)) {
//(*env)->ExceptionDescribe(env);
//}
//(*jvm)->DestroyJavaVM(jvm);
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Prog.class
Type: application/java
Size: 587 bytes
Desc: Prog.class
Url : http://pogo.kaffe.org/pipermail/kaffe/attachments/20041210/1060d414/Prog.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Prog.java
Type: text/java
Size: 142 bytes
Desc: Prog.java
Url : http://pogo.kaffe.org/pipermail/kaffe/attachments/20041210/1060d414/Prog-0001.bin
More information about the kaffe
mailing list