ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Calling RPG from JAVA

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Calling RPG from JAVA

    i did this using the FREE JTOpen tool from IBM. You will need to download it and add the packages to your JAVA project.

    I'm only using Text fields here, but there are also other AS400 conversion types if you need them.

    Code:
    // import the AS400 access classes
    import com.ibm.as400.access.*;
    
    public class CallAS400Program{
    
      CallAS400Program(){
      }
    
      public static void main(String[] args){
        try{
          // declare a new instance of your as400
          AS400 sys = new AS400("myAS400Name","myAS400Profile","myAS400Password");
          
          String addr1,addr2,city,state,zip,zip4,error;
    
          addr1 = "12345 A Street";
          addr2 = "";
          city = "Los Angeles";
          state = "CA";
          zip = "";
          zip4 = "";
          error = "";
          
          // Create AS400 Text objects for the different lengths
          // of parameters you are sending in.
          AS400Text txt30 = new AS400Text(30);
          AS400Text txt20 = new AS400Text(20);
          AS400Text txt2 = new AS400Text(2);
          AS400Text txt5 = new AS400Text(5);
          AS400Text txt4 = new AS400Text(4);
          AS400Text txt3 = new AS400Text(3);
          
          // declare and instantiate  your parameter list.
          ProgramParameter[] parmList = new ProgramParameter[7];
          
          // assign values to your parameters using the AS400Text class to convert to bytes
          // the second parameter is an integer which sets the length of your parameter output
          parmList[0] = new ProgramParameter( txt30.toBytes(addr1),30);
          parmList[1] = new ProgramParameter( txt30.toBytes(addr2),30);      
          parmList[2] = new ProgramParameter( txt20.toBytes(city),20);      
          parmList[3] = new ProgramParameter( txt2.toBytes(state),2);      
          parmList[4] = new ProgramParameter( txt5.toBytes(zip),5);      
          parmList[5] = new ProgramParameter( txt4.toBytes(zip4),4);      
          parmList[6] = new ProgramParameter(txt3.toBytes(error),3);
     
          // declare and instantiate the program.
          // if you don't know the exact program string, you can use: QSYSObjectPathName.toPath("MYLIBRARY","MYPROGRAM","PGM")
          ProgramCall pgm = new ProgramCall(sys,"/QSYS.LIB/MYLIBRARY.LIB/MYPROGRAM.PGM",parmList);
          
          if (pgm.run() != true) {
            AS400Message[] messageList = pgm.getMessageList();
          }else{
              System.out.println("Output Data 0: " + (String)txt30.toObject( parmList[0].getOutputData() ) );
              System.out.println("Output Data 1: " + (String)txt30.toObject( parmList[1].getOutputData() ) );
              System.out.println("Output Data 2: " + (String)txt20.toObject( parmList[2].getOutputData() ) );
              System.out.println("Output Data 3: " + (String)txt2.toObject( parmList[3].getOutputData() ) );
              System.out.println("Output Data 4: " + (String)txt5.toObject( parmList[4].getOutputData() ) );
              System.out.println("Output Data 5: " + (String)txt4.toObject( parmList[5].getOutputData() ) );
              System.out.println("Output Data 6: " + (String)txt3.toObject( parmList[6].getOutputData() ) );
    
              sys.disconnectService(AS400.COMMAND);
           }
         }catch(Exception e) {
           System.out.println(e.toString());
         }
      }
Working...
X