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.
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; } }
javac Demo1.java
This produces a Demo1.class file in the current folder.
$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.
Windows:
cobol demo1.cbl; cbllink demo1.obj
UNIX:
cob -x demo1.cbl
This produces the final COBOL executable file, demo1.exe (Windows) or demo1 (UNIX).
Windows:
demo1.exe
UNIX:
./demo1
---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.