Friday, July 15, 2011

Calling Java from RPG (Iseries / AS400)

The following program shows how to create and manipulate Java objects from within an RPG program.
 
The example will take a string containing the text ‘   hello  ’ and remove the trailing and leading blanks using the java String method trim.
 
     *Call a Java program from RPG  
     h dftactgrp(*no) actgrp(*caller)
 
      *Declare a variable str - Java class type String
     D str             S               O   CLASS(*JAVA:'java.lang.String')
 
      //Prototype for creating a String object
     D makestring      PR              O   EXTPROC(*JAVA:
     D                                      'java.lang.String':
     D                                      *CONSTRUCTOR)
     D                                     CLASS(*JAVA:'java.lang.String')
     D   parm                     65535A   CONST VARYING
 
      *  Prototype the String method getBytes which converts a String to a byte
      *  array.  We can then store this byte array in an alpha field.
      *
     D getBytes        PR         65535A   EXTPROC(*JAVA:
     D                                      'java.lang.String':
     D                                      'getBytes') VARYING
      *
      *  Prototype the String method trim.  It doesn't take any parameters,
      *  but since it is not a static method, must be called using a String
      *  object.
      *
     D trimstring      PR              O   EXTPROC(*JAVA:
     D                                      'java.lang.String':
     D                                      'trim')
     D                                     CLASS(*JAVA:'java.lang.String')
 
     D fld             S             10A     INZ('   hello  ') VARYING
 
      /free
 
        //Call the String constructor
        str = makestring(fld);
 
 
        //Trim the string by calling the Java String trim() method.
        //We will reuse the str field to store the result.
 
        str = trimstring(str);
 
        //Convert the string back to a byte array and store it
        //in fld.  Fld now contains ‘hello     ‘
 
        fld = getBytes(str);
 
        *inlr = *on;
        return;
 
Calling Java from RPG

No comments:

Post a Comment