Accessing COBOL WORKING-STORAGE data items from Java

This topic explains how data items in COBOL WORKING-STORAGE can be accessed from Java™.

Any Java-compatible data items in a COBOL program that fall under the scope of a JAVA-SHAREABLE ON directive are accessible from Java. The directive applies only to 01/77-level items, thus if a JAVA-SHAREABLE ON directive appears in the middle of a group, it will apply to the next 01/77-level item that follows. However, if an 01-level group item falls under the scope of a JAVA-SHAREABLE ON directive, all data items subordinate to the 01-level item in the group can be accessed from Java.

When a data item is shareable with Java, the compiler generates a Java class that defines an interface to both read and write to the memory of the data item from Java code. The interface class is instantiated at run time and accessible through a special class called strg that belongs to the package indicated by the -p option when cjbuild is run, where enterprise.COBOL is the default package name. The strg class will provide an interface for each COBOL program in the application that shares WORKING-STORAGE memory with Java, and for each program there will be an interface for each of the WORKING-STORAGE 01/77-level items that are shareable.

Note:
  1. Because the mechanism for sharing data between COBOL and Java involves EXTERNAL data items in COBOL, data items that are Java-shareable should have a unique name across the entire COBOL part of the application; otherwise, there is a possibility for a name collision. The name of Java-shareable data items must also be 26 characters or less.
  2. Before the WORKING-STORAGE memory of a COBOL program can be accessed from Java, the WORKING-STORAGE of the program should be initialized first. This can only happen if the program is called directly or indirectly from Java.
  3. Any data item that falls under the scope of a JAVA-SHAREABLE ON directive that is not a Java-compatible type will be ignored.

Example

Consider the following Java-shareable group defined in Java-callable COBOL program PROGA:
JAVA-SHAREABLE ON
01 g1.
  03 data1 pic s9(9) comp-5.
  03 data2 pic s9(5)v9(4) comp-3.
01 fl comp-1.
JAVA-SHAREABLE OFF
And consider the following Java-shareable group that is defined in Java-callable COBOL program PROGB:
JAVA-SHAREABLE ON
01 g2.
  03 data1 pic x(20).
  03 data2 pic comp-2.
01 i1 pic s9(9) comp-5.
JAVA-SHAREABLE OFF
Assume the default package enterprise.COBOL was used when building the DLL for the application that uses cjbuild. You can use the following Java code to access these data items:
import enterprise.COBOL.*;
import java.math.*;

public class TestApp
{
  public static void main(String[] args)
  {
    // Make sure COBOL programs have been called at least once
    // to initialize their WORKING-STORAGE
    enterprise.COBOL.progs.PROGA(); // assume no arguments to pass
    enterprise.COBOL.progs.PROGB(); // assume no arguments to pass

    // get data item values from PROGA
    int g1_data1 = enterprise.COBOL.strg.PROGA.G1.data1.get();
    BigDecimal g1_data2 = enterprise.COBOL.strg.PROGA.G1.data2.get();
    float f1 = enterprise.COBOL.strg.PROGA.F1.get();

    // put data items from PROGA
    enterprise.COBOL.strg.PROGA.G1.data1.put(25);
    entprprise.COBOL.strg.PROGA.G1.data2.put(new BigDecimal(25.3645));
    enterprise.COBOL.strg.PROGA.F1.put(3.1415f);

    // get data item values from PROGB
    String g2_data1 = enterprise.COBOL.strg.PROGB.data1.put();
    double g2_data2 = enterprise.COBOL.strg.PROGB.data2.put();
    int i1 = enterprise.COBOL.strg.PROGB.I1.get();
    
    // put data items from PROGB
    enterprise.COBOL.strg.PROGB.data1.put("Hello, world!");
    enterprise.COBOL.strg.PROGB.data2.put(3.1415926);
    enterprise.COBOL.strg.PROGB.I1.put(26);
  }
}

Related references  
JAVA-CALLABLE (Enterprise COBOL for z/OS® Language Reference)  
JAVA-SHAREABLE (Enterprise COBOL for z/OS Language Reference)