JNI JNI_CreateJavaVM hangs Linux

K

kevin

Good Day,

I have a strange behavior starting a Java VM from a C side test program.
On Redhat 9.0, my test program works fine and runs my Java GUI. On
Slackware 9.0 the same code hangs on the JNI_CreateJavaVM. If you look
at the process list you find that 5 - 10 processes with my application
name have been started (at the same time). I have tried to make sure
everything is the exactly same for both distributions. I am using the
same installation of the JDK from Sun NetBeans, my user environment is
the same, My kernel configuration is the same and I am using the same
hardware by swapping IDE drives.

In ideas why and how this can happen and why my app is being spawned
over and over?

#include<jni.h>

#include"NativeCanvas.h"

#include<iostream>


unsigned int *dbuffer;

int main() {


JNIEnv *env;
JavaVM *jvm;
JavaVMInitArgs vm_args;
JavaVMOption options[1];
jint res;
jclass cls;
jclass cls2;
jmethodID mid;
jstring jstr;
jobjectArray args;
char classpath[1024];
jint error;

vm_args.version=JNI_VERSION_1_2;
// error = JNI_GetDefaultJavaVMInitArgs(&vm_args);

// printf("vm_args = %d\n",error);

//function pointers int (*pt2Function) (float, char, char);
void (*ptInit) (JNIEnv *, jobject, jint, jint);
void (*ptPaint) (JNIEnv *, jobject);


vm_args.nOptions=1;
options[0].optionString = "-Djava.class.path=.";
vm_args.options=options;
vm_args.ignoreUnrecognized=JNI_FALSE;

printf("env= %d\n",env);

res = JNI_CreateJavaVM(&jvm,(void**) &env, &vm_args);


if(res < 0){
fprintf(stderr,"Can't create JavaVM\n");
exit(1);
}


printf("env= %d\n",env);

ptInit = &Java_NativeCanvas_initNative;
ptPaint = &Java_NativeCanvas_paintNative;

printf("ptIint = %d, ptPaint = %d \n",ptInit,ptPaint);

JNINativeMethod methods[] = {
{"initNative","(II)V",(void *)ptInit},
{"paintNative","()V",(void *)ptPaint}
};

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

mid = env->GetStaticMethodID(cls, "main" ,"([Ljava/lang/String;)V");

if(mid == 0){
fprintf(stderr,"Can't find testmain.main\n");
exit(1);
}

jstr=env->NewStringUTF("");

if(jstr == 0){
fprintf(stderr,"Out of memory\n");
exit(1);
}

args = env->NewObjectArray(0,env->FindClass("java/lang/String"),jstr);
if(args == 0){
fprintf(stderr,"Out of memory\n");
exit(1);
}

printf("cls= %d\n",cls);
cls2 = env->FindClass("NativeCanvas");
printf("cls2= %d\n",cls2);
if(cls2 == 0){
fprintf(stderr, "Can't find class 2 \n");
exit(1);
}

int reg = env->RegisterNatives(cls2,methods,2);
if(reg != 0)
{
printf("error Native Reg %d\n",reg);
exit(1);
}

env->CallStaticVoidMethod(cls,mid,args);
jvm->DestroyJavaVM();

}
 
R

Roedy Green

In ideas why and how this can happen and why my app is being spawned
over and over?

No, however, I have noticed many apps fail if you spawn copies too
rapidly in succession. An app waking up is prepared to deal with
another copy already up and awake, but not another just groggily
staggering to its feet before the first coffee.

To find out, just put in some debug code and a stack trace to see who
is doing all that spawning.
 
G

Gordon Beaton

I have a strange behavior starting a Java VM from a C side test
program. On Redhat 9.0, my test program works fine and runs my Java
GUI. On Slackware 9.0 the same code hangs on the JNI_CreateJavaVM.
If you look at the process list you find that 5 - 10 processes with
my application name have been started (at the same time). I have
tried to make sure everything is the exactly same for both
distributions. I am using the same installation of the JDK from Sun
NetBeans, my user environment is the same, My kernel configuration
is the same and I am using the same hardware by swapping IDE drives.

In ideas why and how this can happen and why my app is being spawned
over and over?

First, are you sure that your application is being spawned over and
over? Remember that threads on Linux show up in the process table, and
look exactly like processes.

Second, use fprintf(stderr,"...") to display error messages to stderr
instead of stdout. You can't be sure that your program hasn't in fact
passed printf("..."), since output to stdout (which printf() uses) is
buffered and may not appear immediately. You should also use %p to
display pointers, not %d.

That said, you should look for differences in the two runtime
environments. Compare the output of ldd on the executable file in each
environment, and the content of /proc/<PID>/maps and
/proc/<PID>/environ for the running processes.

What exact commands do you use to compile and link your launcher? How
do you tell the launcher where to find the JVM dynamic libraries?

/gordon
 
P

perry

does it work on other platforms ?

when dealing with operating system code (aka c/c++) you need to be very
disciplined in isolating the known from the unknown and as anyone well
tell you when dealing with the earlier languages, your error messages
may not always be as they seem...

- perry
Good Day,

I have a strange behavior starting a Java VM from a C side test program.
On Redhat 9.0, my test program works fine and runs my Java GUI. On
Slackware 9.0 the same code hangs on the JNI_CreateJavaVM. If you look
at the process list you find that 5 - 10 processes with my application
name have been started (at the same time). I have tried to make sure
everything is the exactly same for both distributions. I am using the
same installation of the JDK from Sun NetBeans, my user environment is
the same, My kernel configuration is the same and I am using the same
hardware by swapping IDE drives.

In ideas why and how this can happen and why my app is being spawned
over and over?

#include<jni.h>

#include"NativeCanvas.h"

#include<iostream>


unsigned int *dbuffer;

int main() {


JNIEnv *env;
JavaVM *jvm;
JavaVMInitArgs vm_args;
JavaVMOption options[1];
jint res;
jclass cls;
jclass cls2;
jmethodID mid;
jstring jstr;
jobjectArray args;
char classpath[1024];
jint error;

vm_args.version=JNI_VERSION_1_2;
// error = JNI_GetDefaultJavaVMInitArgs(&vm_args);

// printf("vm_args = %d\n",error);

//function pointers int (*pt2Function) (float, char, char);
void (*ptInit) (JNIEnv *, jobject, jint, jint);
void (*ptPaint) (JNIEnv *, jobject);


vm_args.nOptions=1;
options[0].optionString = "-Djava.class.path=.";
vm_args.options=options;
vm_args.ignoreUnrecognized=JNI_FALSE;

printf("env= %d\n",env);

res = JNI_CreateJavaVM(&jvm,(void**) &env, &vm_args);


if(res < 0){
fprintf(stderr,"Can't create JavaVM\n");
exit(1);
}


printf("env= %d\n",env);

ptInit = &Java_NativeCanvas_initNative;
ptPaint = &Java_NativeCanvas_paintNative;

printf("ptIint = %d, ptPaint = %d \n",ptInit,ptPaint);

JNINativeMethod methods[] = {
{"initNative","(II)V",(void *)ptInit},
{"paintNative","()V",(void *)ptPaint}
};

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

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

jstr=env->NewStringUTF("");

if(jstr == 0){
fprintf(stderr,"Out of memory\n");
exit(1);
}

args = env->NewObjectArray(0,env->FindClass("java/lang/String"),jstr);
if(args == 0){
fprintf(stderr,"Out of memory\n");
exit(1);
}

printf("cls= %d\n",cls);
cls2 = env->FindClass("NativeCanvas");
printf("cls2= %d\n",cls2);
if(cls2 == 0){
fprintf(stderr, "Can't find class 2 \n");
exit(1);
}

int reg = env->RegisterNatives(cls2,methods,2);
if(reg != 0)
{
printf("error Native Reg %d\n",reg);
exit(1);
}

env->CallStaticVoidMethod(cls,mid,args);
jvm->DestroyJavaVM();

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top