Calling System.getProperty()

I want to run:

System.getProperty( property );

where property is some key, like "java.class.path", using C++.Here's my attempt:

void Jni::listProperty(char *property )

{

jclass clsSystem= FindClass("java/lang/System");

jclass clsPrintStream = FindClass("java/io/PrintStream");

jmethodID midPrintLn= GetMethodID( clsPrintStream,"println","(Ljava/lang/String;)V");

jmethodID midGetProperty = GetStaticMethodID( clsSystem,"getProperty","(Ljava/lang/String;)Ljava/lang/String;");

// Create a new String and call the method to get the property value.

jstring jProperty = env->NewStringUTF(property);

if ( env->ExceptionOccurred() ) env->ExceptionDescribe();

jstring result= (jstring) env->CallStaticObjectMethod(clsSystem, midGetProperty, jProperty);

if ( env->ExceptionOccurred() ) env->ExceptionDescribe();

char *s = (char *)env->GetStringUTFChars(result, NULL);

if ( env->ExceptionOccurred() ) env->ExceptionDescribe();

cout <<"property is: " << s << endl;

env->CallVoidMethod(clsPrintStream, midPrintLn, result);

env->DeleteLocalRef( clsSystem );

}

There are two problems:

First, I try to store the output of getProperty() into a jstring, convert that to a C string with getStringUTFChars and then print the property value.The output appears to be blank. I don't see how though. I should've gotten some kind of error if the key didn't exist in for the Map that getProperty consults.

Secondly, I try to print the output by handing the String directly to PrintStream.printlin(). But JNI seems to hate that. I get errors on the call to CallVoidMethod().

[2238 byte] By [caffeinea] at [2007-11-26 13:46:24]
# 1

> The output appears to be blank.

Your code seems fine. What is the property you are trying to read?

If the property is java.class.path it's possible that the result is only "."

(maybe you didn't see the result at the console).

Did yo try it in pure Java to see if the result is the same.

> env->CallVoidMethod(clsPrintStream, midPrintLn, result);

Can't do that. You have to use an object reference and not a class reference

to call the CallVoidMethod() JNI function.

Regards

jfbrierea at 2007-7-8 1:21:26 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2

> > The output appears to be blank.

>

> Your code seems fine. What is the property you are trying to read?

> If the property is java.class.path it's possible that the result is only "."

> (maybe you didn't see the result at the console).

I'm having trouble loading one of my class files in my "real" program, so I

created a little test program to print out the java.class.path property of the

System class.

> Did you try it in pure Java to see if the result is the same.

No, but I just did.:-)The results are as expected.

> > env->CallVoidMethod(clsPrintStream, midPrintLn, result);

>

> Can't do that. You have to use an object reference

> and not a class reference

> to call the CallVoidMethod() JNI function.

Doh!Yeah, that makes sense. I'm still a Java newbie (and *really* new to

the JNI).

I just now realized that I was setting Java.class.path, not java.class.path.

Ooops!I just fixed that, and now my output is as expected.*whew*My

class path appears to be set correctly.

I still can't load my class file. But at least now I know what my

java.class.path is set to!:-)

Thanks!

caffeinea at 2007-7-8 1:21:26 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3
OK. The classpath needs to point to the jar file itself, not the directory that contains the jar file.I guess this is because a jar file implements part of the directory structure itself; it's not simply an archive file.Mission accomplished. :)
caffeinea at 2007-7-8 1:21:26 > top of Java-index,Java HotSpot Virtual Machine,Specifications...