Pointer Class

An intrinsic problem with passing Java strings to COBOL is that Java strings are variable length whereas COBOL strings are fixed length. For COBOL to modify a predefined fixed-length field, you need to wrap the instance in a new instance of the Pointer class. The Pointer class is in the com.microfocus.cobol.lang package.

For example, here is a COBOL program that defines a PIC X(20) data item:

showbytes.cbl:
       working-storage section.
       linkage section.
       01 lnk-string     pic x(20).
       procedure division using lnk-string.
          display "lnk-string = " lnk-string.
          move "Hello from COBOL - 1" to lnk-string.
          exit program returning 0.

The corresponding Java code would be:

showbytes.java:

import com.microfocus.cobol.RuntimeSystem;
import com.microfocus.cobol.lang.Pointer;
import com.microfocus.cobol.lang.ParameterList;

public class showbytes
{
  public static void main(String[] args) throws Exception
  {
   String myString = new String("Hello to COBOL");
   Pointer ptr2String = new Pointer(myString, 20);

   RuntimeSystem.cobcall("showbytes", 
	         new ParameterList().add(ptr2String));
		   	
   myString = ptr2String.toString();

   System.out.println(myString = ["+myString+"]");
  }  
}