Example 2 - Java Accessing COBOL Working Storage Items

This example demonstrates a Java program accessing and manipulating COBOL working storage items from a native COBOL program.

Run the following instructions from a shell prompt.

Important: Ensure that the current working folder and the folder containing libjvm.so is available on the LD_LIBRARY_PATH (or LIBPATH on AIX) environment variable.
  1. Create the following COBOL file (demo2.cbl):
          $set sourceformat(variable)
           program-id. "demo2".
           working-storage section.
           >>JAVA-SHAREABLE ON 
           01 grp1.
              03 i1 pic 9(8) comp-5 value 88888888.
              03 i2 pic 9(8) comp-5 value 12345678.
           >>JAVA-SHAREABLE OFF
           01 grp2.
              03 p1 pic x.
              03 p2 pic 9. 
           procedure division.
           display "start".
           display "value of shared CBL grp is: "
           display grp1::i1.
           display grp1::i2.
           end program.
  2. Create an src2 subfolder to your current working folder.
  3. Compile the file with the necessary directives:
    cob -yo libdemo2.so -C "java-package-name(com.mycompany.demo2) java-output-path(src2) java-gen-strg java-gen-progs ibmcomp" -Q -znoexecstack demo2.cbl  
    Note: For AIX platforms, omit the -Q -znoexecstack flags.

    The COBOL library file is generated, and its supporting class files are generated using the com.mycompany.demo2 package name and placed in the src2 folder.

  4. Create the following Java source file (Demo2.java):
    import com.mycompany.demo2.*;
    
    public class Demo2
     {
       public static void main(String[] args)
        {
          System.out.println("--COBOL items now accessible in Java--"); 
          int i1 = strg.demo2.grp1.i1.get();
          int i2 = strg.demo2.grp1.i2.get();      
          System.out.println("-- COBOL item i1 = " + i1);
          System.out.println("-- COBOL item i2 = " + i2);
          strg.demo2.grp1.i1.put(77777777);
          System.out.println("-- i1 updated from Java = " + strg.demo2.grp1.i1.get());
        }
     }
    The Java code imports the namespace that the COBOL compilation used. You should also ensure that the current working folder and the src2 folder are on the CLASSPATH.
  5. Compile, and then run the Java bytecode:
    javac Demo2.java
    java Demo2
    Note: On certain Linux platforms, you may need to run the Java bytecode using the -Xrs option if you receive a 115 Unexpected signal (Signal 4) error message.

    The following output is produced:

    --COBOL items now accessible in Java--
    -- COBOL item i1 = 88888888
    -- COBOL item i2 = 12345678
    -- i1 updated from Java = 77777777

    The code and the output shows that the COBOL program has shared two PIC 9 COMP-5 items with the Java program. The Java program has used the get method to view the value of the COBOL data (to do this, the COMP-5 item was mapped to int types, as per Mapping COBOL Items and Java Types). The Java program then used the put method to change the value of the COBOL item (where again the mapping process was used, under the covers).