How to call Java main(String[] args) method from RPGLE - iSeries (AS400)

You may already know how to call a java class using RUNJVA or JAVA from iSeries. I am going show you a sample RPGLE code to call the java main() method. Just remember if you want a result back from the Java program you must call a method other than main() as it doesn't return any Object.

Sample Java Program to be called from RPGLE

package com.as400samplecode;                           
public class SimpleJava {                              
 public static void main(String[] args) {              
  String firstName = args[0].trim();                   
  String lastName = args[1].trim();                    
  String age = args[2].trim();                         
  System.out.println("First Name: " + firstName);      
  System.out.println("Last Name: " + lastName);        
  System.out.println("Age: " + age);                   

Call Java main() using RUNJVA

RUNJVA CLASS(com.as400samplecode.SimpleJava) PARM('AS400' 'Programmer' '31')

Call Java main() using JAVA

JAVA CLASS(com.as400samplecode.SimpleJava) PARM('AS400' 'Programmer' '31')

Call Java main() using RPGLE

h DftActgrp(*NO) ActGrp(*Caller)                                       
D Exc_Cmd         PR                  extpgm('QCMDEXC')                
D  command                     200A   const                            
D  length                       15P 5 const                            
 * Prototype for SimpleJava Main() function                            
d SimpleMain      PR                  EXTPROC(*JAVA:                   
d                                     'com.as400samplecode.SimpleJava':
d                                     'main')                          
d                                     STATIC                           
d String                          O   CLASS(*JAVA:'java.lang.String')  
d                                     dim(3)                           
d                                     Const                            
 * Prototype for Java String Object                                    
d crtString       PR              o   EXTPROC(*JAVA:                   
d                                             'java.lang.String':      
d                                             *CONSTRUCTOR)            
d RPGBytes                      50A   Const Varying                    
 * Input/Output object                                                 
d args            s               o   Class(*JAVA:'java.lang.String')  
d                                     dim(3)
     Exc_Cmd('CD (''/{directory1}/{directory2}'')': 200);    
     args(1) = crtString('AS400');         
     args(2) = crtString('Programmer');            
     args(3) = crtString('31');             
     *inlr = *on;                           

Java main method takes a varying size array of strings. Here we have simulated a string array using RPGLE dim() keyword.

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.