Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > JNI C++ side test code.

Reply
Thread Tools

JNI C++ side test code.

 
 
Roedy Green
Guest
Posts: n/a
 
      04-12-2004
On Tue, 13 Apr 2004 07:57:25 -0700, kevin <(E-Mail Removed)>
wrote or quoted :

>
>I am trying to build a C++ side test code for some JNI methods.


What I do is test the C++ side with a C++ test harness that uses no
JNI at all. Then add the JNI at bit at a time, and test from a Java
test harness.

You may find that sufficient. There are two problems with JNI -- the
learning curve, and just being sufficiently fastidious. Given the
terrifying convolutions that go on in JNI at the ASM level, I'm not
sure if tracing it would enlighten more than confuse.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
 
 
 
Kevin
Guest
Posts: n/a
 
      04-12-2004
Roedy Green wrote:
> On Tue, 13 Apr 2004 07:57:25 -0700, kevin <(E-Mail Removed)>
> wrote or quoted :
>
>
>>I am trying to build a C++ side test code for some JNI methods.

>
>
> What I do is test the C++ side with a C++ test harness that uses no
> JNI at all. Then add the JNI at bit at a time, and test from a Java
> test harness.
>
> You may find that sufficient. There are two problems with JNI -- the
> learning curve, and just being sufficiently fastidious. Given the
> terrifying convolutions that go on in JNI at the ASM level, I'm not
> sure if tracing it would enlighten more than confuse.
>
> --
> Canadian Mind Products, Roedy Green.
> Coaching, problem solving, economical contract programming.
> See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.



Thank for the thoughts. I found that my test was failing on the Method
registration for the native code. I had an extra whitespace in the
signiture. I should have been checking the return.




 
Reply With Quote
 
 
 
 
kevin
Guest
Posts: n/a
 
      04-13-2004

I am trying to build a C++ side test code for some JNI methods. The
following code was gleened from web examples and works up until the
env->FindClass("GuiFrame").

I get the Java VM and environment but nothing I do to setup the
classpath in or outside of the code seems to return my java
GuiFrame.class. Does anyone see any obvious problem or have a static
C++ code that loads a javaVM and Java Class from the Native side?



#include<jni.h>

#include"NativeCanvas.h"

#include<iostream>

main() {


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


vm_args.version=JNI_VERSION_1_4;

error = JNI_GetDefaultJavaVMInitArgs(&vm_args);

printf("vm_args = %d\n",error);
vm_args.nOptions=1;

options[0].optionString="-Djava.class.path=.:/home/kevin/NativeCanvas";
vm_args.options=options;
vm_args.ignoreUnrecognized=JNI_FALSE;


res = JNI_CreateJavaVM(&jvm,(void**) &env, &vm_args);
printf("env= %d, %d\n",env,res);
const JNINativeMethod methods[] = {
{"paintNative","()V ",(void*)&Java_NativeCanvas_paintNative},
{"initNative","(II)V ",(void*)&Java_NativeCanvas_initNative}
};

env->RegisterNatives(env->FindClass("NativeCanvas"),methods,2);

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


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/StringV");

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

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

}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: JNI: Error loading DLL from JNI DDL vasanth Java 0 01-25-2005 11:03 AM
Re: JNI: Error loading DLL from JNI DDL vasanth Java 0 01-25-2005 11:01 AM
Porting JNI Windows under JNI LINUX + Wine ? Pasturel Jean-Louis Java 5 03-03-2004 07:50 PM
IBM's JNI fails where Sun's JNI works Alex Hunsley Java 4 11-04-2003 10:34 AM
test test test test test test test Computer Support 2 07-02-2003 06:02 PM



Advertisments