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().

