JNI setproperty

Discussion in 'Java' started by EExtasEE, Jan 20, 2005.

  1. EExtasEE

    EExtasEE Guest

    hi,
    i want to change my JVM properties("user.dir", "os.name" etc...) using JNI.
    So the setProperty() method have 2 parameters that's why i have tried to
    do this :

    sys = (*env)->FindClass (env, "java/lang/System");

    meth = (*env)->GetStaticMethodID (env, sys, "setProperty",
    "(Ljava/lang/String;)Ljava/lang/String;","(Ljava/lang/String;)Ljava/lang/String;");

    (*env)->CallStaticObjectMethod(env, sys, meth, (*env)->NewStringUTF
    (env, &key),(*env)->NewStringUTF (env, &newVal));

    I don't understand why it doesn't work.

    Can anyone help me?
    Thanks by advance
     
    EExtasEE, Jan 20, 2005
    #1
    1. Advertising

  2. On Thu, 20 Jan 2005 15:21:57 +0100, EExtasEE wrote:
    > i want to change my JVM properties("user.dir", "os.name" etc...)
    > using JNI. So the setProperty() method have 2 parameters that's why
    > i have tried to do this :
    >
    > sys = (*env)->FindClass (env, "java/lang/System");
    >
    > meth = (*env)->GetStaticMethodID (env, sys, "setProperty",
    > "(Ljava/lang/String;)Ljava/lang/String;","(Ljava/lang/String;)Ljava/lang/String;");


    GetStaticMethodID() takes 4 arguments, not 5.

    According to javap, the signature for System.setProperty() is this:

    "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"

    > (*env)->CallStaticObjectMethod(env, sys, meth, (*env)->NewStringUTF
    > (env, &key),(*env)->NewStringUTF (env, &newVal));
    >
    > I don't understand why it doesn't work.


    What happens instead? Be specific.

    Also, how were key and newVal declared and initialized? The fact that
    you use addressof (&) in the calls to NewStringUTF() is suspicious.

    If you'd checked the return values from the JNI functions, you'd know
    when one has failed.

    Why are you doing this from JNI?

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Jan 20, 2005
    #2
    1. Advertising

  3. EExtasEE

    EExtasEE Guest

    Gordon Beaton a écrit :
    > On Thu, 20 Jan 2005 15:21:57 +0100, EExtasEE wrote:
    >
    >>i want to change my JVM properties("user.dir", "os.name" etc...)
    >>using JNI. So the setProperty() method have 2 parameters that's why
    >>i have tried to do this :
    >>
    >>sys = (*env)->FindClass (env, "java/lang/System");
    >>
    >>meth = (*env)->GetStaticMethodID (env, sys, "setProperty",

    That should work with pointers to String, i i successfully implemented
    pointer using getProperty()

    ///////////////////////////////////////////////////////////////////////
    // Sample //
    ///////////////////////////////////////////////////////////////////////
    const char *fmt ;
    char key[1024]; //Nom de la clef
    char Val[1024]; //Nouvelle valeur de la clef

    printf("Type key name \t");
    scanf("%s",&key);
    sys = (*env)->FindClass (env, "java/lang/System");
    met = (*env)->GetStaticMethodID (env, sys, "getProperty",
    "(Ljava/lang/String;)Ljava/lang/String;");
    ret = (jstring)(*env)->CallStaticObjectMethod(env, sys, met,
    (*env)->NewStringUTF (env, &Val));
    fmt=(char *) (*env)->GetStringUTFChars(env, ret, 0);
    printf("The value is :\t\t\t\t%s\n\n", fmt);
    break;
    ////////////////////////////////////////////////////////////////////////

    Why are you doing this from JNI?<--Im learning JNI

    >>"(Ljava/lang/String;)Ljava/lang/String;","(Ljava/lang/String;)Ljava/lang/String;");

    >
    >
    > GetStaticMethodID() takes 4 arguments, not 5.
    >
    > According to javap, the signature for System.setProperty() is this:
    >
    > "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"
    >
    >
    >>(*env)->CallStaticObjectMethod(env, sys, meth, (*env)->NewStringUTF
    >>(env, &key),(*env)->NewStringUTF (env, &newVal));
    >>
    >>I don't understand why it doesn't work.

    >
    >
    > What happens instead? Be specific.
    >
    > Also, how were key and newVal declared and initialized? The fact that
    > you use addressof (&) in the calls to NewStringUTF() is suspicious.
    >
    > If you'd checked the return values from the JNI functions, you'd know
    > when one has failed.
    >
    > Why are you doing this from JNI?
    >
    > /gordon
    >
     
    EExtasEE, Jan 20, 2005
    #3
  4. On Thu, 20 Jan 2005 17:29:19 +0100, EExtasEE wrote:
    > That should work with pointers to String, i i successfully
    > implemented pointer using getProperty()


    While '&key' and 'key' have the same *value* in this case (and both
    should work), their types are different:

    char key[1024];

    key has type 'char*' (pointer to char)
    &key has type '*char[]' (pointer to array of char)

    Even though both expressions will work (as you've discovered), the
    correct form is /without/ the addressof operator '&' in order to match
    the type expected by e.g. NewStringUTF(). If you had turned on
    compiler warnings (as you normally should), your compiler would have
    warned you about the type mismatch.

    Did my other suggestions solve your problem? In particular, the
    signature you specified for System.setProperty() was wrong.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Jan 21, 2005
    #4
    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.
Similar Threads
  1. Hugo
    Replies:
    6
    Views:
    464
    Gordon Beaton
    Feb 26, 2004
  2. dpr
    Replies:
    0
    Views:
    398
  3. Dale
    Replies:
    0
    Views:
    2,432
  4. kaeli
    Replies:
    5
    Views:
    8,201
    kaeli
    Oct 12, 2004
  5. Emmanuel Doute
    Replies:
    4
    Views:
    720
    Ryan Stewart
    Jan 11, 2005
Loading...

Share This Page