This example demonstrates Java calling procedural COBOL with JNI service calls.

The COBOL program is compiled and linked as follows, where a typical installation path for COBOL is /usr/lpp/cobol and a typical installation path for Java is /usr/lpp/java. Replace with your own paths for your environment.


/usr/lpp/cobol/bin/cob2 -c -qdll,thread -I /usr/lpp/cobol/include stringTest.cbl

/usr/lpp/cobol/bin/cob2 -bdll -o libStringTest.so stringTest.o /usr/lpp/java/IBM/J7.1/bin/j9vm/libjvm.x /usr/lpp/cobol/lib/igzcjava.x -I /usr/lpp/cobol/include stringTest.cbl

stringTest.cbl

Process pgmname(longmixed),lib,dll,thread

       IDENTIFICATION DIVISION.
       PROGRAM-ID.    "Java_StringTest_printStrings" is recursive.

       ENVIRONMENT DIVISION.
       CONFIGURATION SECTION.

       INPUT-OUTPUT SECTION.
       DATA DIVISION.
       Local-storage section.
       01 I pic s9(9) binary.
       01 P pointer.
       01 SAelement pic s9(9) binary.
       01 SAelementlen pic s9(9) binary.
       01 Sbuffer pic X(50) .
       01 rc Pic s9(9) Comp-5.


       LINKAGE SECTION.
       01 SA  PIC S9(9) BINARY.
       01 SAlen pic s9(9) binary.
       01 ENV-PTR   USAGE POINTER.
       01 OBJECT-REF  PIC S9(9) BINARY.

       COPY JNI SUPPRESS.

       PROCEDURE DIVISION USING BY VALUE ENV-PTR //two JNI 'hidden' parms
                                     OBJECT-REF
                                     SA
                           RETURNING SAlen.

           Set address of JNIEnv to ENV-PTR
           Set address of JNINativeInterface to JNIEnv


           Call GetArrayLength using by value ENV-PTR SA
              returning SAlen
           Display " >> COBOL method entered array len: " SAlen

           Perform varying I from 0 by 1 until I = SAlen
              Call GetObjectArrayElement
                 using by value ENV-PTR SA I
                 returning SAelement
              Call "GetStringPlatformLength"
                 using by value ENV-PTR
                                SAelement
                                address of SAelementlen
                                0
                 returning rc
              Display "Returned from GetStringPlatformLength"
              If rc Not = zero Then
              Display "Error occurred retrieving len of jstring object"
              Stop run
              Else
              Display "The length of returned string is:"
              SAelementlen
              End-if
              Call "GetStringPlatform"
                 using by value ENV-PTR
                                SAelement
                                address of Sbuffer
                                length of Sbuffer
                                0
              Display Sbuffer(1:SAelementlen)
           End-perform

            .
           GOBACK.

The following Java program StringTest.java is compiled and run with the following commands:

/usr/lpp/java/IBM/J7.1/bin/javac StringTest.java
/usr/lpp/java/IBM/J7.1/bin/java -Djava.library.path=. StringTest

StringTest.java

class StringTest
{
   static {
     System.loadLibrary("StringTest");
   }

static native int printStrings(java.lang.String[] SA);

public static void main(String argv[]) throws Exception
   {
    int i;
    String[] a = new String[3];
    a[0] = "John";
    a[1] = "White";
    a[2] = "1234567890";
    StringTest st = new StringTest();
    i = StringTest.printStrings(a);
    System.out.println("Number of elements printStrings read:" +i);
    return;
   }
} 

1 comment on"Example: Java calling procedural COBOL"

  1. JAVA program supposed to run on zIIP. In above example the called COBOL runs under JAVA. Does it means it will run in ZIIP too ?

Join The Discussion

Your email address will not be published. Required fields are marked *