Examples: COBOL applications that run using the java command

The following examples show COBOL class definitions that contain a factory method called main.

In each case, main has no RETURNING phrase and has a single USING parameter, an object reference to a class that is an array with elements of type java.lang.String. You can run these applications by using the java command.

Displaying a message


 cbl dll,thread
 Identification Division.
 Class-id. CBLmain inherits Base.
 Environment Division.
 Configuration section.
 Repository.
     Class Base is "java.lang.Object"
     Class stringArray is "jobjectArray:java.lang.String"
     Class CBLmain is "CBLmain".
*
 Identification Division.
 Factory.
  Procedure division.
*
   Identification Division.
   Method-id. "main".
   Data division.
   Linkage section.
   01 SA usage object reference stringArray.
   Procedure division using by value SA.
     Display " >> COBOL main method entered"
     .
   End method "main".
 End factory.
 End class CBLmain.

Echoing the input strings


 cbl dll,thread,pgmname(longmixed),ssrange
 Identification Division.
 Class-id. Echo inherits Base.
 Environment Division.
 Configuration section.
 Repository.
     Class Base is "java.lang.Object"
     Class stringArray is "jobjectArray:java.lang.String"
     Class jstring is "java.lang.String"
     Class Echo is "Echo".
*
 Identification Division.
 Factory.
  Procedure division.
*
   Identification Division.
   Method-id. "main".
   Data division.
   Local-storage section.
   01 SAlen        pic s9(9) binary.
   01 I            pic s9(9) binary.
   01 SAelement    object reference jstring.
   01 SAelementlen pic s9(9) binary. 
   01 Sbuffer      pic X(65535).
   01 P            pointer.
   Linkage section.
   01 SA           object reference stringArray.
   Copy "JNI.cpy" suppress.
   Procedure division using by value SA.
     Set address of JNIEnv to JNIEnvPtr
     Set address of JNINativeInterface to JNIEnv
     Call GetArrayLength using by value JNIEnvPtr SA
       returning SAlen
     Display "Input string array length: " SAlen
     Display "Input strings:"
     Perform varying I from 0 by 1 until I = SAlen
       Call GetObjectArrayElement
         using by value JNIEnvPtr SA I
         returning SAelement
       Call "GetStringPlatformLength"
         using by value JNIEnvPtr
                        SAelement
                        address of SAelementlen
                        0
       Call "GetStringPlatform"
         using by value JNIEnvPtr
                        SAelement
                        address of Sbuffer
                        length of Sbuffer
                        0
       Display Sbuffer(1:SAelementlen)
     End-perform
     .
   End method "main".
 End factory.
 End class Echo.