Sample COBOL and Java programs:
cobprod.cbl, addlist.cbl, and
TestApp.java
The COBPROD application consists of cobprod.cbl,
addlist.cbl, and TestApp.java
z/OS®
UNIX files.
Sample COBOL program: cobprod.cbl
* Initialize a working-storage item with some
* product information to be shared with Java
identification division.
program-id. cobprod.
data division.
working-storage section.
* Product info table, shared with Java
01 prod-info-data.
03 filler.
05 filler pic x(20) value 'chair'.
05 filler pic s9(9)v9(2) value 29.99.
03 filler.
05 filler pic x(20) value 'table'.
05 filler pic s9(9)v9(2) value 45.98.
03 filler.
05 filler pic x(20) value 'bed'.
05 filler pic s9(9)v9(2) value 149.45.
03 filler.
05 filler pic x(20) value 'blanket'.
05 filler pic s9(9)v9(2) value 19.99.
03 filler.
05 filler pic x(20) value 'sofa'.
05 filler pic s9(9)v9(2) value 239.99.
>>java-shareable on
01 prod-info redefines prod-info-data.
03 prod-list occurs 5 times.
05 prod-name pic x(20).
05 prod-price pic s9(9)v9(2).
>>java-shareable off
01 prod-list-size pic s9(9) comp-5 value 5.
linkage section.
01 prod-list-size-out pic s9(9) comp-5.
>>java-callable
procedure division returning prod-list-size-out.
MainProgram.
* Populate table
move prod-list-size to prod-list-size-out
goback.
end program cobprod.
Sample COBOL program: addlist.cbl
cbl pgmname(longmixed)
* Receives a list of three packed decimals and calculates
* and returns the sum
identification division.
program-id. 'addlist'.
data division.
working-storage section.
local-storage section.
01 idx pic s9(9) comp-5.
linkage section.
01 pricelist.
03 price pic s9(9)v9(2) comp-3 occurs 3 times.
01 listsize pic s9(9) comp-5.
01 result pic s9(9)v9(2) comp-3.
>>java-callable
procedure division using by reference pricelist
by value listsize
returning result.
MainProgram.
* Compute sum of list
display '<<COBOL: addlist: entered>>'
compute result = 0
perform varying idx from 1 by 1
until idx > listsize
compute result = result + price(idx)
end-perform
call 'Java.TestApp.printResult' using result
display '<<COBOL: addlist: exited>>'
goback.
end program 'addlist'.
Sample Java program:
TestApp.java
import enterprise.COBOL.*;
import java.math.*;
public class TestApp
{
public static void main(String[] args)
{
BigDecimal[] prices = new BigDecimal[3];
System.out.println("<<Java: TestApp.main: entered>>");
System.out.println();
// Now access a product table from COBOL
System.out.println("");
System.out.println("Java: Access COBOL product info from COBPROD");
int tableSize = enterprise.COBOL.progs.COBPROD();
System.out.println("Java: prod table size = " + tableSize);
for (int i = 0; i < tableSize; i++)
{
System.out.print("Java: Product name = " +
enterprise.COBOL.strg.COBPROD.PROD_INFO.PROD_NAME[i].get());
System.out.print(" Price = $");
System.out.println(enterprise.COBOL.strg.COBPROD.PROD_INFO.PROD_PRICE[i].get());
if (i < 3)
{
prices[i] = enterprise.COBOL.strg.COBPROD.PROD_INFO.PROD_PRICE[i].get();
}
}
System.out.println();
// Call out to COBOL to sum the list
System.out.println("Java: call COBOL addlist to add prices");
System.out.println();
BigDecimal result = enterprise.COBOL.progs.addlist(prices, 3);
System.out.println();
System.out.println("<<Java: TestApp.main: exited>>");
}
public static void printResult(BigDecimal bd)
{
System.out.println("Java (called from COBOL): printResult: " +
"total price is: $" + bd);
}
}
