|
In a previous post (http://archive.midrange.com/rpg400-l/200403/msg00073.html) the question was asked about invoking Java's "System.out.println" method from an RPG program. Having done considerable RPG w/Java work, I wanted to test this approach as, in the past, I've used C's "printf" function to echo information to the terminal or STDOUT. I prefer to use 'printf' verses RPG's DSPLY opcode, as the maximum length DSPLY supports is 52 bytes. In any case, what follows is an example of invoking "System.out.println" from RPG. I should point out, however, that, at least on my system, this approach doesn't behave the same as using "printf". Whereas, "printf" can/will (based on activation group specification) issue the "Press ENTER to end terminal session." statement, "System.out.println" does not. I've tried numerous options and approaches but none have produced the same results. My suspicion is that STDOUT is handled differently under the covers, based on the respective interface. Guess I'll continue to use "printf". Here's the code sample (for Stu): H THREAD(*SERIALIZE) BndDir('QC2LE') DFTACTGRP(*no) ACTGRP(*new) * D/DEFINE OS400_JVM_12 * D/DEFINE JNI_COPY_FIELD_FUNCTIONS D/COPY qsysinc/QRPGLESRC,JNI ****************************************************************** * ****************************************************************** Dnewstring PR O EXTPROC(*JAVA: D 'java.lang.String': D *CONSTRUCTOR) D CLASS(*JAVA:'java.lang.String') D 16384A varying CONST * Djprintf PR EXTPROC(*JAVA: D 'java.io.PrintStream': D 'println') D O class(*java:'java.lang.String') * Dwritebytes PR EXTPROC(*JAVA: D 'java.io.PrintStream': D 'write') D 16384A CONST varying Doffset 10I 0 value Dlen 10I 0 value * D printf PR EXTPROC('printf') D * value options(*string) * DgetJniEnv PR * DdestroyJVM PR N ****************************************************************** * Signatures and variables used by JNI calls ****************************************************************** * 'Ljava/io/PrintStream;' signature DJINPS C x'4c6a6176612f696f2f- D 5072696e7453747265616d3b' * 'java/lang/System' DJLSys C x'6a6176612f6c616e672f- D 53797374656d' * 'out' Dout C x'6f7574' ****************************************************************** * Java objects used ****************************************************************** DPrtString S O CLASS(*JAVA:'java.lang.String') DJLSysClass S O CLASS(*JAVA:'java.lang.Class') Dstdout_fid S O CLASS(*JAVA:'java.lang.Object') Dstdout S O CLASS(*JAVA:'java.lang.Object') DJNI_Error S O CLASS(*JAVA: D 'java.lang.Throwable') * C EVAL PrtString = newString('call to JPRINTF') * C eval JNIEnv_P = getJniEnv() * C eval JLSysClass = FindClass (JNIEnv_P:JLSys) * C EVAL stdout_Fid=GetStaticFieldID (JNIEnv_P C : JLSysClass : out : JINPS) * C EVAL stdout = GetStaticObjectField (JNIEnv_P C :JLSysclass:stdout_Fid) * C callp jPrintf(STDOUT:PrtString) * C callp writebytes(STDOUT: C 'call to WRITEBYTES'+x'25':0:19) * C callp Printf('TSTJAVAOUT Ended...'+x'15') * C callp destroyJVM() C move '1' *inlr * D/COPY *libl/QRPGLESRC,getJniEnv D/COPY *libl/QRPGLESRC,destroyJVM Regards, Michael Rooney P.S. For those interested, an example of this technique in "C" can be found in the August 2000 edition of JDC Techtips at http://java.sun.com/developer/TechTips/txtarchive/2000/July00_StuH.txt .
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.