Example 4 - Java Calling COBOL and Accessing Working Storage Items

This example uses a Java program to call into two COBOL programs, accessing working-storage items from one of those programs.

As well as demonstrating a combination of examples 2 and 3, this example also demonstrates the use of the genjava utility, which is required because the Java program calls into multiple COBOL programs.

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 (demo4.cbl):
          $set nsymbol(national) 
           >>JAVA-CALLABLE
           program-id. "demo4".
           working-storage section.
           >>JAVA-SHAREABLE ON
           01 Int1 pic 9(9) comp-5 value 10.
           01 Alpha1 pic x(100) value "Pink".
           01 Group1.
              03 colour1 pic x(10) occurs 10 value "Red" "Green" "Blue".
           01 Num1 pic S9(5)v9(5) comp-3 value 3141.59265.
           01 Nat1 pic n(20) value n"Unicode" national.
           >>JAVA-SHAREABLE OFF
           01 ed-num1 pic +9(5).9(5).
           procedure division.
               display "--In COBOL Program demo4--"
               display "Int1= " Int1
               display "Alpha1= " Alpha1
               display "colour1(1)= "colour1(1)
               display "colour1(2)= "colour1(2)
               display "colour1(3)= "colour1(3)
               move num1 to ed-num1
               display "Num1= " ed-num1
               display "Nat1= " Nat1
           goback.
  2. Create the following COBOL file (demo5.cbl):
          $set nsymbol(national) 
           >>JAVA-CALLABLE
           program-id. "demo5".
           working-storage section.
           >>JAVA-SHAREABLE ON
           01 Group1.
              03 Group2.
                05 item1 pic x(10) value "Purple".
                05 Group3.
                  07 item2 pic x(10) value "Green".
           >>JAVA-SHAREABLE OFF
           01 3item pic 9(10).
           procedure division.
               display "--In COBOL Program demo5--"
               display "Item1= " item1
               display "Item2= " item2
           goback.
  3. Create an src4 subfolder to your current working folder.
  4. Compile the demo4.cbl file with the necessary Java-specific directives:
    cob demo4.cbl -yc -C "java-package-name(com.mycompany.demo4) java-output-path(src4)"  

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

  5. Compile the demo5.cbl file with the necessary Java-specific directives:
    cob demo5.cbl -yc -C "java-package-name(com.mycompany.demo4) java-output-path(src4)"  

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

  6. Create the required library file from the compiled object files:
    cob -yo libapp4.so demo4.o demo5.o  -Q -znoexecstack 
    Note: For AIX platforms, omit the -Q -znoexecstack flags.

    The libapp4.so library file is generated.

  7. Create the required progs.java and strg.java files using the genjava utility:
    genjava libapp4 -p demo4 demo5 -s demo4 demo5 -o src4 -k com.mycompany.demo4
    Note: You must use the genjava utility (as opposed to the JAVA-GEN-PROGS and JAVA-GEN-STRG directives as multiple COBOL files are callable and sharing data.
  8. Create the following Java source file (Demo4.java):
    import com.mycompany.demo4.*;
    import java.math.BigDecimal;
    
    public class Demo4
    {
        public static void main(String[] args)
        {
          System.out.println("--Java--");
          int i = strg.demo4.Int1.get();
          System.out.println("Int1= " + i);
          strg.demo4.Int1.put(10201);
    
          String s = strg.demo4.Alpha1.get();
          System.out.println("Alpha1= " + s);
          strg.demo4.Alpha1.put("Purple");
    
          strg.demo4.Group1.colour1[2].put("Yellow");
          strg.demo4.Group1.colour1[3].put("Cyan");
    
          BigDecimal num1 = strg.demo4.Num1.get();
          System.out.println("Num1= " + num1);
          strg.demo4.Num1.put(new BigDecimal("1579.1113"));
    
          String n = strg.demo4.Nat1.get();
          System.out.println("Nat1= " + n);
          strg.demo4.Nat1.put("abcdeFGHIJ");
    
          String i1 = strg.demo5.Group1.Group2.item1.get();
          System.out.println("Item1= " + i1);
          strg.demo5.Group1.Group2.item1.put("Orange");
    
          String i2 = strg.demo5.Group1.Group2.Group3.item2.get();
          System.out.println("Item1= " + i2);
          strg.demo5.Group1.Group2.Group3.item2.put("Yellow");
    
          progs.demo4();
          progs.demo5();
        }
    }

    The Java code imports the namespace that the COBOL compilation used. You should also ensure that the current working folder and the src4 folder are on the CLASSPATH.

  9. Compile, and then run the Java bytecode:
    javac Demo4.java
    java Demo4
    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:

    --Java--
    Int1= 10
    Alpha1= Pink
    Num1= 3141.59265
    Nat1= Unicode
    Item1= Purple
    Item1= Green
    --In COBOL Program demo4--
    Int1= 0000010201
    Alpha1= Purple
    colour1(1)= Red
    colour1(2)= Green
    colour1(3)= Yellow
    Num1= +01579.11130
    Nat1= abcdeFGHIJ
    --In COBOL Program demo5--
    Item1= Orange
    Item2= Yellow

    The code and the output show the Java program accessing the working-storage data in the COBOL programs, even declaring Java variables using the values of the COBOL items. The Java code also calls both of the COBOL programs.