Example 3 - Java Calling COBOL Programs

This example demonstrates a Java program running code 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 (demo3.cbl):
          $set sourceformat(variable)
           >>JAVA-CALLABLE
           program-id. "demo3" .
    
           working-storage section.
           01 i pic 9(9) comp-5.
           01 primes.
             03 pic 9(9) comp-5 occurs 10 value 2,3,5,7,11,13,17,19,23,27.
    
           linkage section.
           01 ltable1.
             03 str pic X(100) occurs 4.
           01 lint1 pic 9(9) comp-5.
           01 ltable2.
             03 int1 pic 9(9) comp-5 occurs 10.
           procedure division using ltable1 by value lint1 returning ltable2.
               perform varying i from 1 by 1 until i > 4
                   display "COBOL " i " " str(i)
               end-perform
               display "COBOL " lint1
               move primes to ltable2
           goback.
  2. Create an src3 subfolder to your current working folder.
  3. Compile the file with the necessary directives:
    cob -yo libdemo3.so -C "java-package-name(com.mycompany.demo3) java-output-path(src3) java-gen-progs" -Q -znoexecstack demo3.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.demo3 package name and placed in the src3 folder.

  4. Create the following Java source file (Demo3.java):
    import com.mycompany.demo3.*;
    
    public class Demo3
    {
        public static void main(String[] args)
        {
          String[] s = {"hello", "there", "pink", "green"};
          int int1 = 23;
          System.out.println("---------demo3---------");
          int[] i = com.mycompany.demo3.progs.demo3(s, int1);
          System.out.println("Hello from Java");
          for (int k=0; k<i.length; k++)
              System.out.println(i[k]);
    
        }
    }

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

  5. Compile, and the run the Java bytecode:
    javac Demo3.java
    java Demo3
    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:

    ---------demo3---------
    COBOL 0000000001 hello
    COBOL 0000000002 there
    COBOL 0000000003 pink
    COBOL 0000000004 green
    COBOL 0000000023
    Hello from Java
    2
    3
    5
    7
    11
    13
    17
    19
    23
    27

    The code and the output show that the Java program calls into the demo3 COBOL program when declaring the i integer array. It passes in a couple of arguments used by the COBOL code, and then the COBOL code also returns a table/array, which the Java program then displays.