Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > JNI setproperty

Reply
Thread Tools

JNI setproperty

 
 
EExtasEE
Guest
Posts: n/a
 
      01-20-2005
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/StringLjava/lang/String;","(Ljava/lang/StringLjava/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
 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      01-20-2005
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/StringLjava/lang/String;","(Ljava/lang/StringLjava/lang/String;");


GetStaticMethodID() takes 4 arguments, not 5.

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

"(Ljava/lang/String;Ljava/lang/StringLjava/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
 
Reply With Quote
 
 
 
 
EExtasEE
Guest
Posts: n/a
 
      01-20-2005
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/StringLjava/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/StringLjava/lang/String;","(Ljava/lang/StringLjava/lang/String;");

>
>
> GetStaticMethodID() takes 4 arguments, not 5.
>
> According to javap, the signature for System.setProperty() is this:
>
> "(Ljava/lang/String;Ljava/lang/StringLjava/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
>

 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      01-21-2005
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
 
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
using jsp:setProperty on non-simple types. Emmanuel Doute Java 4 01-11-2005 09:51 PM
JSP: error page exception.getMessage returning null for setProperty exceptions kaeli Java 5 10-12-2004 02:34 PM
JBoss Lomboz Eclipse JSP Parsing Error on jsp:setProperty Dale Java 0 10-01-2004 04:58 PM
newbye. setProperty exception bad format. how to handle ? dpr Java 0 06-03-2004 07:53 AM
Invoking System.setProperty() two times on the same property Hugo Java 6 02-26-2004 11:41 AM



Advertisments