JNI setproperty

E

EExtasEE

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
 
G

Gordon Beaton

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
 
E

EExtasEE

Gordon Beaton a écrit :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
 
G

Gordon Beaton

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
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top