Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JNI : Get the Class of a Static Field of a Static Class

807603Dec 6 2007 — edited Dec 10 2007
TODO: Call System.out.println from JNI.
PROBLEM: Once I have the System Class and out FieldID, how do I get a class reference to System.out so I can call its method.

I have seen all sorts of samples on the web that don�t make any sense. It doesn�t make sense for me to be able to just FindClass(�java/lang/PrintStream�). How does the Java runtime know which instance of PrintStream associated with System I am looking for (ie err or out). Many online samples just do this.

I don�t see any methods in the JNI that take a static FieldID and return a class (unless I am missing something obvious here). So the question remains. How is this done? Here is a snippet of what I have so far:

-----

jint jniPrintln( JNIEnv pJRT, jboolean bErr, const char szLine )
{
int rc = -1;
jfieldID fid = NULL;
jmethodID mid = NULL;
jclass jcSystem = NULL;
jclass jcStream = NULL;
jstring jzEntry;

if( szLine==NULL || szLine[0]=='\0' )
{return 0;}

if( NULL==(jcSystem=(*pJRT)->FindClass(pJRT, "java/lang/System")) )
{return -1;}
else if( NULL==(fid=(*pJRT)->GetStaticFieldID(pJRT,jcSystem,bErr?"err":"out")) )
{goto cleanup;}
/**
TODO:
*** Somehow, set jcStream=class(java/lang/PrintStream) from fieldID for System.out
**/
else if( NULL==(mid=(*pJRT)->GetMethodID( pJRT, jcStream, "println", "(Ljava/lang/String;)V" )) )
{goto cleanup;}
else if( NULL==(jzEntry=(*pJRT)->NewStringUTF(pJRT,aEntry)) )
{goto cleanup;}

(*pJRT)->CallStaticMethod(pJRT,jcStream,mid,jzEntry);

if( JNI_FALSE==jniTriggerExcept( pJRT, "Call to System.out.println() FAILED", NULL, JNI_FALSE ) )
{rc = 0;}

cleanup:
if( jzEntry!=NULL )
{(*pJRT)->DeleteLocalRef( pJRT, jzEntry );}
if( jcStream!=NULL ) {(*pJRT)->DeleteLocalRef( pJRT, jcStream ); }
if( jcSystem!=NULL )
{(*pJRT)->DeleteLocalRef( pJRT, jcSystem );}
return rc;
}

//xx James A. Renton
//xx Beverly, MA
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 7 2008
Added on Dec 6 2007
7 comments
679 views