Java Calling COBOL Programs

You can configure your native COBOL programs to be accessible to Java programs. The Java program can call into the main program entry point and execute the COBOL code.

To make the COBOL program callable, you use the JAVA-CALLABLE directive, which can either be set as a source directive (as shown below) or as a conventional directive set with $set or from the command line.

The COBOL source must be compiled to a library file (a .dll file), where the inclusion of the JAVA_CALLABLE directive generates a number of files (some of which are generated under the covers, and so not visible to the user); these are required to form part of the run unit driven by the Java application accessing the program.

       >>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.
       ...
       PROCEDURE DIVISION USING LTABLE1 BY VALUE LINT1 RETURNING LTABLE2.
       ...       

In addition to the JAVA-CALLABLE directive, you must also include the JAVA-GEN-PROGS directive, to produce a progs.java wrapper class; this is another file required for the final run unit, to enable the Java program to call the COBOL program.

Two additional directives, JAVA-OUTPUT-PATH and JAVA-PACKAGE-NAME, also help to expose the COBOL program(s) to Java. For example, an excerpt from the Java program may look something like this:
import com.mycompany.demo3.*;

public class Demo3
{
 ...
 int[] i = com.mycompany.demo3.progs.demo3(s, int1);
 ...
}

where the COBOL program(s) being shared was compiled with JAVA-PACKAGE-NAME(com.mycompany.demo3); also ensure that the value for JAVA-OUTPUT-PATH is on the CLASSPATH when you run your Java code. This excerpt demonstrates the Java code declaring an integer array with the values returned from the demo3 COBOL program - the progs element of the declaration is instantiating the progs.java wrapper class, which in turn calls the demo3 program with the required arguments.

For a working example of a COBOL program being called by a Java program, from the command line, see Example 3 - Java Calling COBOL Programs .

If you are calling into more than one COBOL source program, you need to create the progs.java file using the genjava utility (instead of using the JAVA-GEN-PROGS directive); see The genjava Utility for more information.

The calling of COBOL programs can be used alongside the JAVA SHAREABLE interoperability method (see Java Accessing COBOL Working Storage Items ), where your Java applications can drive both the COBOL code and COBOL data; see Example 4 - Java Calling COBOL and Accessing Working Storage Items.