Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: JNI CallStaticVoidMethod crash when Java variables declared outside method

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post JNI CallStaticVoidMethod crash when Java variables declared outside method

    I have started learning JNI to call C/Objective C from Java.
    I have a simple java class like this where int i is declared and assigned within the setValue method:

    // --------JAVA CLASS EXAMPLE 1

    public class Test {

    private native void _start();

    private void setValue(string str) {
    int i;
    i = 6;
    System.out.println(str);
    }
    }

    The corresponding C/Objective C code does a callback to Java using CallStaticVoidMethod.

    // ----------------JNI function in Objective C

    JNIEXPORT void JNICALL Java_org_test_try__start
    (JNIEnv * env, jobject obj){


    JNF_COCOA_ENTER(env);

    jobject myObject = (*env)->NewGlobalRef(env, obj);
    jclass myClass = (*env)->GetObjectClass(env, myObject);
    jmethodID myMethod = (*env)->GetMethodID(env, myClass, "setValue", "(Ljava/lang/StringV");
    char* str = "Confirmed";
    jstring js = (*env)->NewStringUTF( env, str );
    (*env)->CallStaticVoidMethod(env, myClass, myMethod, js);

    JNF_COCOA_EXIT(env);
    }

    This all works fine.
    However, if I declare the int i in the java class and not inside the method as follows, the JVM crashes.

    // --------JAVA CLASS EXAMPLE 2

    public class Test {

    private int i;

    private native void _start();

    private void setValue(string str) {;
    i = 6;
    System.out.println(str);
    }
    }

    It SEEMS the callback can't access the int variable when it is declared at class level.
    Why is this and how can I fix that?


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: JNI CallStaticVoidMethod crash when Java variables declared outside method

    Hi SGARCH,

    By the looks of things you are invoking CallStaticVoidMethod on a Non-Static method. This should be erroneous.

    Change this to be CallVoidMethod, and you might have more luck. Looks like the JVM might be trying to invoke it as a static, in which case the reference to i in the second example wouldn't work, as i is not static.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JNI CallStaticVoidMethod crash when Java variables declared outside method

    Many thanks for your help - so simple now that I see it. CallVoidMethod works fine.

Similar Threads

  1. java JNI help required
    By Phoenix23 in forum Java Native Interface
    Replies: 1
    Last Post: March 22nd, 2013, 09:05 PM
  2. java JNI help required
    By Phoenix23 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 22nd, 2013, 08:27 PM
  3. Declared variables not carrying over
    By evanscott815 in forum AWT / Java Swing
    Replies: 6
    Last Post: October 4th, 2012, 11:52 AM
  4. Problems With Method Calls and Variables
    By connor3452 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 17th, 2012, 05:46 AM
  5. Why I can't return two variables in one method?
    By netlync in forum Java Theory & Questions
    Replies: 3
    Last Post: January 13th, 2012, 02:39 AM