JNI C++ side test code.

Discussion in 'Java' started by Roedy Green, Apr 12, 2004.

  1. Roedy Green

    Roedy Green Guest

    On Tue, 13 Apr 2004 07:57:25 -0700, kevin <>
    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.
    Roedy Green, Apr 12, 2004
    #1
    1. Advertising

  2. Roedy Green

    Kevin Guest

    Roedy Green wrote:
    > On Tue, 13 Apr 2004 07:57:25 -0700, kevin <>
    > 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.
    Kevin, Apr 12, 2004
    #2
    1. Advertising

  3. Roedy Green

    kevin Guest

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

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

    }
    kevin, Apr 13, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page