Created On:  31 May 2012


Is it possible to pass a Java String array to COBOL in a manner so that the COBOL program can receive this as a COBOL table in the linkage section as if it were being called by another COBOL program?


Yes, it is possible to pass a Java String array to a COBOL subprogram through it's linkage section using the COBOL type Pointer defined in it's Java support classes.

An example Net Express 5.1 project is attached to this article.  The following is the description of this project:

This example program shows how a simple Java string array can be passed between Java and COBOL using the COBOL support for the Pointer class.
This allows the array to be passed directly as a COBOL array so COBOL source code does not need to be modified to use any special data type or java support class such as jarray.

This project has a java main program within the class
This program creates a Java String array and then concatenates the elements of the array into a StringBuffer and passes this as a Pointer to the COBOL program.

The COBOL program will then display the contents of the string array, repopulate the array with new values and return back to Java. Java will then convert the data passed back to a Java String array and display the new contents.       

This can only be done when the Java strings are of a fixed length as this is how they are stored in COBOL. 
In order to compile the .java program in this project you will need to ensure that the COBOL/Java environment is setup as documented in the Net Express documentation under Programming--> Java-->Java and COBOL-->Using Java and COBOL.   

To debug the COBOL program simply set a breakpoint in the source and run the main program. 

      $set reentrant case intlevel"4"                                        
       id division.                                                          
       program-id.  passarray.                                               
       thread-local-storage section.                                         
       copy "javatypes.cpy".                                                 
       01  sub-1                pic x(4) comp-5  value 1.                    
       01  new-table-data.                                                   
           05                   pic x(5)         value "00000".              
           05                   pic x(5)         value "11111".              
           05                   pic x(5)         value "22222".              
           05                   pic x(5)         value "33333".              
           05                   pic x(5)         value "44444".              
           05                   pic x(5)         value "55555".              
           05                   pic x(5)         value "66666".              
           05                   pic x(5)         value "77777".              
           05                   pic x(5)         value "88888".              
           05                   pic x(5)         value "99999".              
       01  new-table redefines new-table-data.                               
           05  new-table-field  pic x(5) occurs 10 times.                    
       linkage section.                                                      
       01  lnk-table.                                                        
           05  lnk-table-field  pic x(5) occurs 10 times.                    
       procedure division.                                                   
           entry "cobentry" using lnk-table.                                 
      *    call "CBL_DEBUGBREAK".                                            
           display "In COBOL program passarray.dll"                          
           display "Table items passed from Java:"                           
           perform varying sub-1 from 1 by 1                                 
              until sub-1 > 10                                               
              display lnk-table-field(sub-1)                                 
              move new-table-field(sub-1) to lnk-table-field(sub-1)          
           exit program returning 0.                                         

And the calling Java program looks like:

import com.microfocus.cobol.RuntimeSystem;                                                        
import com.microfocus.cobol.lang.Pointer;                                                         
public class javapassarray                                                                        
    public static void main(String[] args) throws Exception                                       
       int count, startpos, endpos;                                                               
       //  The following call will load the COBOL passarray.dll                                   
       //  Create a simple Java string array consisting of 10 5 byte elements.                    
       String[] stringArray = { "AAAAA","BBBBB","CCCCC","DDDDD","EEEEE",                          
           "FFFFF","GGGGG","HHHHH","IIIII","JJJJJ" };                                             
       //  We will create a single StringBuffer item by concatenating all elements of the stringArr
       StringBuffer tempString = new StringBuffer();                                              
       tempString.ensureCapacity(50);    // 10 * 5 chars                                          
       for (count = 0; count < 10; count )                                                       
       //  Setup the parms to pass to COBOL. In this case we use the Pointer object to pass the add
       Pointer stringPtr = new Pointer(tempString.toString(), 50);                                
       Object parms[] = { stringPtr };                                                            
       //  Call the COBOL entry point and pass the parameter.                                     
       //  The following code simply reloads the stringArray from the string passed back from COBOL
       startpos = 0;                                                                              
       for (count = 0; count < 10; count )                                                       
          endpos = startpos 4;    //  Elements are each 5 characters in length.                 
          stringArray[count] = tempString.toString().substring(startpos, endpos);                 
          startpos = 5;  //  Setup next item.                                                    
       //  Display the contents of the Java Array after returning from COBOL.                     
       System.out.println("Table items returned from COBOL:");                                    
       for (count = 0; count < 10; count )                                                       
          System.out.println("value: " stringArray[count]);                                     
          Console console = System.console();                                                     
          String str = console.readLine("Press Enter");