Example 1 - COBOL Calling Java Static Method

This example demonstrates a native COBOL program executing a Java static method.

When the COBOL program is run, it passes the Java program a string array that is processed by Java, and also makes a call to Java where the Java program returns an array which the COBOL program processes.

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 Java file (Demo1.java):
    import java.math.BigDecimal;
    import java.util.Arrays;
    
    public class Demo1
       {
        public static void static1(String[] d)
        {
            System.out.println("---Output from Java Demo1.static1 method---");
            for (int i = 0; i < d.length; i++)
            {
                System.out.println(d[i]);
            }
            Arrays.sort(d);
        }
        /* select colours from array */
        public static String[] static2(int[] s)
        {
            System.out.println("---Output from Java Demo1.static2 method---");
            for (int i: s)
                System.out.println(i);
            String[] rainbow = {"Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet"};
            String[] ret = {rainbow[s[0]], rainbow[s[1]], rainbow[s[2]], rainbow[s[3]], rainbow[s[4]]};
            return ret;
        }
    }
  2. Ensure that the current working folder is on the CLASSPATH.
  3. Compile Demo1.java:
    javac Demo1.java

    This produces a Demo1.class file in the current folder.

  4. In the same folder, create the following COBOL file (demo1.cbl):
          $set sourceformat(variable) nsymbol(national)
           78 MAX_OCC value 5.
           01 i pic xxxx comp-5.
           01 grp2.
              03 nat1 pic n(10) national occurs MAX_OCC
                     value n"Red" n"Green" n"Blue" n"Orange" n"Indigo".
           01 grp3.
             03 utf1 pic u(10) occurs MAX_OCC.
           01 grp4.
             03 num2 pic xxxx comp-5 occurs MAX_OCC
                      value 1 2 4 5 6. 
           procedure division.
          *> Sort COBOL array
            call "java.Demo1.static1" using grp2
            display "---Output from COBOL---"
            perform varying i from 1 by 1 until i > 5
                display nat1(i)
            end-perform
          *> Select colours of the rainbow from input array
          *> Careful Java has 0 based array indexes
             call "java.Demo1.static2" using grp4 returning grp3
             display "---Output from COBOL---"
             perform varying i from 1 by 1 until i > 5
                display utf1(i)
             end-perform.
  5. Compile and link the COBOL file:
    cob -x demo1.cbl  

    This produces the final COBOL executable file, demo1.

  6. Run the executable file:
    ./demo1
    The program output, showing output from both the COBOL program and Java program, should look like this:
    ---Output from Java Demo1.static1 method---
    Red
    Green
    Blue
    Orange
    Indigo
    ---Output from COBOL---
    Blue
    Green
    Indigo
    Orange
    Red
    ---Output from Java Demo1.static2 method---
    1
    2
    4
    5
    6
    ---Output from COBOL---
    Orange
    Yellow
    Blue
    Indigo
    Violet

    The code and the output shows the COBOL code executing the static1 method, processing some COBOL operations, then executing the static2 method, and processing some more COBOL operations.