How to Call a Java Program from RPGLE

Java methods are called the same way as RPGLE or C functions. You must define the Prototype to the Java Method. To understand how to call a Java method you must first understand that in Java everything is an Object.

The Object Data Type

Fields that can store objects are declared using the O data type. To declare a field of type O, code O in column 40 of the D-specification and use the CLASS keyword to provide the class of the object.

The CLASS keyword accepts two parameters: CLASS(*JAVA:class_name)

*JAVA identifies the object as a Java object. Class_name specifies the class of the object. It must be a character literal or named constant, and the class name must be fully qualified. The class name is case sensitive. For example,

To declare a field that will hold an object of type BigDecimal:
D bdnum S O CLASS(*JAVA:'java.math.BigDecimal')

To declare a field that will hold an object of type String:
D string S O CLASS(*JAVA:'java.lang.String')

Prototyping a Java Method


EXTPROC(*JAVA:class-name:method-name)

EXTPROC Specifies a method that is written in Java.
  • The first parameter is *JAVA. 
  • The second parameter is a character constant containing the class of the method. 
  • The third parameter is a character constant containing the method name.


D add             PR              O   ExtProc(*JAVA:                    
D                                         'java.math.BigDecimal':       
D                                         'add')                        
D                                     Class(*JAVA:'java.math.BigDecimal')
D  BigD2                          O   Class(*JAVA:'java.math.BigDecimal')
D                                     Const

The next line in RED is part of the definition of the return value. The CLASS keyword completes the definition of the return value defined on the first line of the prototype.

Next line in ORANGE is the input parameter definition, BigD2. This input parm is a Java object from the class java.math.BigDecimal. Since this method will not change the value of this parameter, I use the Const keyword to tell it so and make it a little more efficient.


Define Java Constructor


A java constructor is a special method that has the same name as the class. This method can be used to instantiate a class (create a new class instance).
D String2BigD     PR              O   ExtProc(*JAVA:                   
D                                         'java.math.BigDecimal':      
D                                         *CONSTRUCTOR)                
D                                     Class(*JAVA:'java.math.BigDecimal')
D  Str                            O   Class(*JAVA:'java.lang.String')  
D                                     Const      

Now let's get to an actual sample code and see it working !

Sample Java Program GetItem to be called from RPGLE

package com.as400samplecode; 

public class GetItem { 

 public Item getItemInfo(String itemNumber) { 

  Item item = new Item(); 

  if(itemNumber.equalsIgnoreCase("abc")) { 

   item.setItemNumber(itemNumber); 
   item.setDescription1("Description 1 for ABC"); 
   item.setDescription2("Description 2 for ABC"); 
   item.setPrice(100); 

  } 
  else { 

   item.setItemNumber(itemNumber); 
   item.setDescription1("Description 1 for " + itemNumber); 
   item.setDescription2("Description 2 for " + itemNumber); 
   item.setPrice(999); 

  } 

  return item; 

 } 

} 

Sample Java Object Item to be called from RPGLE

package com.as400samplecode; 

public class Item { 

 private String itemNumber = null; 
 private String description1 = null; 
 private String description2 = null; 
 private int price = 0; 

 public String getItemNumber() { 
  return itemNumber; 
 } 
 public void setItemNumber(String itemNumber) { 
  this.itemNumber = itemNumber; 
 } 
 public String getDescription1() { 
  return description1; 
 } 
 public void setDescription1(String description1) { this.description1 = description1; 
 } 
 public String getDescription2() { 
  return description2; 
 } 
 public void setDescription2(String description2) { 
  this.description2 = description2; 
 } 
 public int getPrice() { 
  return price; 
 } 
 public void setPrice(int price) { 
  this.price = price; 
 } 

}

Sample RPGLE code calling Java Program

h DftActgrp(*NO) ActGrp(*Caller)         
* Prototype for Java String Object                                        
d crtString       PR              o   EXTPROC(*JAVA:                       
d                                             'java.lang.String':          
d                                             *CONSTRUCTOR)                
d RPGBytes                      30A   Const Varying                        
                                                                           
 * Prototype for Java String's getBytes method                             
d cvtToBytes      PR            30A   EXTPROC(*JAVA:                       
d                                             'java.lang.String':          
d                                             'getBytes')                  
d                                     Varying                              
                                                                        
 * Item object                                                          
d Item            s               o   Class(*JAVA:'com.as400samplecode.Item') 
                                                                        
 * Prototype for Item object                                            
d crtItem         PR              o   EXTPROC(*JAVA:                    
d                                             'com.as400samplecode.Item':     
d                                             *CONSTRUCTOR)             
                                                                        
 * Prototype for Item's Description1 Method                             
d getDesc1        PR              o   EXTPROC(*JAVA:                    
d                                             'com.as400samplecode.Item':     
d                                             'getDescription1')        
d                                     Class   (*JAVA:'java.lang.String')
                                                                        
 * Prototype for Item's Description1 Method                             
d getDesc2        PR              o   EXTPROC(*JAVA:                    
d                                             'com.as400samplecode.Item':     
d                                             'getDescription2')        
d                                     Class   (*JAVA:'java.lang.String')
                                                                        
 * Prototype for Item's Description1 Method                             
d getPrice        PR            10I 0 EXTPROC(*JAVA:                    
d                                             'com.as400samplecode.Item':       
d                                             'getPrice')                 
                                                                          
 * GetItem object                                                         
d GetItem         s               o   Class(*JAVA:'com.as400samplecode.GetItem')
                                                                          
 * Prototype for GetItem object                                           
d crtGetItem      PR              o   EXTPROC(*JAVA:                      
d                                             'com.as400samplecode.GetItem':    
d                                             *CONSTRUCTOR)               
                                                                          
 * Prototype for GetItem's GetItemInfo Method                             
d getItemInfo     PR              o   EXTPROC(*JAVA:                      
d                                             'com.as400samplecode.GetItem':    
d                                             'getItemInfo')              
d                                     Class   (*JAVA:'com.as400samplecode.Item')
d String                          o   Class   (*JAVA:'java.lang.String')  
d                                             Const                       
                                                                          
d $item_          s               o   Class(*JAVA:'java.lang.String')     
d $desc1_         s               o   Class(*JAVA:'java.lang.String')     
d $desc2_         s               o   Class(*JAVA:'java.lang.String')     
                                                                          
d $item           s             30a                              
d $desc1          s             30a                              
d $desc2          s             30a                              
d $price          s             10s 0                            
 *                                                               
 *----- Main Routine                                             
 *                                                               
c                   eval      $item_ = crtString(%trim($item))   
c                   eval      Item = crtItem()                   
c                   eval      GetItem = crtGetItem()             
                                                                 
c                   eval      Item = getItemInfo(GetItem:$item_) 
                                                                 
c                   eval      $desc1_ = getDesc1(Item)           
c                   eval      $desc2_ = getDesc2(Item)           
c                   eval      $price  = getPrice(Item)           
                                                                 
c                   eval      $desc1 = cvtToBytes($desc1_)       
c                   eval      $desc2 = cvtToBytes($desc2_)       
                                                                 
c     $desc1        dsply                                        
c     $desc2        dsply                                        
c     $price        dsply                                        
c                   eval      *inlr = *on        
c                   return                       
 *                                               
 *----- Initial Routine                          
 *                                               
c     *inzsr        begsr                        
                                                 
c     *entry        plist                        
c                   parm                    $item
                                                 
c                   endsr                        

Java and RPG Definitions and Data Types


The data types of the parameters and the returned value of the method are specified in the same way as they are when prototyping a subprocedure, but the data types actually map to Java data types. The following table shows the mappings of ILE RPG data types to and from Java data types.

Java Data Type ILE RPG Data Type RPG Definitions
boolean indicator N
byte 1 integer 3I 0
character 1A
byte[] character length > 1 (See 3.) nA
array of character length=1 (See 4.) 1A DIM(x)
date D
time T
timestamp Z
short 2-byte integer 5I 0
char UCS-2 length=1 1C
char[] UCS-2 length>1 (See 3.) nC
array of UCS-2 length=1 (See 4.) 1C DIM(x)
int 4-byte integer 10I 0
long 8-byte integer 20I 0
float 4-byte float 4F
double 8-byte float 8F
any object object O CLASS(x)
any array array of equivalent type (See 4.) DIM(x)
Notes:

  1. When a Java byte type is converted to or from a character (1A) data type, ASCII conversion occurs. When a Java byte type is converted to or from an integer (3I) data type, ASCII conversion does not occur. 
  2. For arrays of any type in Java, you can declare an array of the equivalent type in RPG. However, note that you cannot use an array of character length greater than 1 or UCS-2 length greater than 1 data types.
  3. For UCS-2 length greater than 1 and character length greater than 1 data types, the VARYING keyword is allowed. In general, it's recommended to use the VARYING keyword, since Java byte[] and char[] cannot be declared with a fixed length.
  4. For RPG array data types, OPTIONS(*VARSIZE) should normally be coded for array parameters, since Java arrays cannot be declared with a fixed length.

Zoned, Packed, Binary, and Unsigned data types are not available in Java. If you pass a Zoned, Packed, Binary, or Unsigned field as a parameter, the compiler will do the appropriate conversion, but this may result in truncation and/or loss of precision.

When calling a method, the compiler will accept arrays as parameters only if the parameter is prototyped using the DIM keyword.

In Java, the following data types can only be passed by value:

  • boolean
  • byte
  • int
  • short
  • long
  • float
  • double

Parameters of these types must have the VALUE keyword specified for them on the prototype.

Note that objects can only be passed by reference. The VALUE keyword cannot be specified with type O. Since arrays are seen by Java as objects, parameters mapping to arrays must also be passed by reference. This includes character and byte arrays. The CONST keyword can be used.

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.