### cjbuild Utility Output Example Source: https://www.ibm.com/docs/en/cobol-zos/6.4.0?topic=clrnocatij-step-2-running-cjbuild-utility-build-dll-java-stub-programs This output shows an example of the cjbuild utility's execution, detailing the configuration parameters and files processed during the COBOL/Java build. It includes paths for Java and COBOL installations, as well as included files for Java calls to COBOL and Java-shareable stubs. ```text Running cjbuild COBOL/Java build tool ===================================== PROGRAM_FILE = none LIB_BASE_NAME = app1 COBOL_DIR = ./out DLL_OUT_DIR = //COBOL.LOAD JAVA_CLASS_DIR = ./class JAVA_SRC_DIR = . JAR_FILE = libapp1.jar PACKAGE_NAME = enterprise.COBOL INTEROP_MODE = MIX_31_64 JAVA_HOME: /usr/lpp/java/J11.0_64 COBOL_INSTALL_DIR: /home/userid/cobol/igyv6r4 User env STEPLIB: USERID.IGY.V6R4M0.SIGYCOMP:CEE.SCEERUN Including file in app DLL: [java-calls-cobol stub] COBPROD_java_native_app1 Including file in app DLL: [java-shareable stub ] COBPROD_java_native_WS_app1 Including file in app DLL: [java-calls-cobol stub] addlist_java_native_app1 ``` -------------------------------- ### Example: Using FILE STATUS and INVALID KEY Source: https://www.ibm.com/docs/en/cobol-zos/6.4.0?topic=errors-handling-in-input-output-operations Illustrates how to combine the file status code with the INVALID KEY phrase to get specific reasons for input/output statement failures in VSAM files. ```COBOL 01 FILE-STATUS-KEY PIC XX. PROCEDURE DIVISION. MAIN-LOGIC. OPEN OUTPUT MY-VSAM-FILE. IF FILE-STATUS-KEY NOT = '00' DISPLAY "Error opening file. Status: " FILE-STATUS-KEY STOP RUN END-IF. MOVE 'SOME-KEY-VALUE' TO KEY-FIELD. MOVE 'DATA-TO-WRITE' TO DATA-FIELD. WRITE MY-VSAM-RECORD INVALID KEY DISPLAY "INVALID KEY error occurred. File Status: " FILE-STATUS-KEY IF FILE-STATUS-KEY = '22' *> Duplicate key DISPLAY "Reason: Duplicate key." ELSE IF FILE-STATUS-KEY = '23' *> Boundary violation DISPLAY "Reason: Boundary violation." ELSE DISPLAY "Reason: Unknown error." END-IF GO TO END-PROGRAM NOT AT END IF FILE-STATUS-KEY NOT = '00' DISPLAY "Error writing record. Status: " FILE-STATUS-KEY GO TO END-PROGRAM END-IF DISPLAY "Record written successfully." END-WRITE. END-PROGRAM. CLOSE MY-VSAM-FILE. STOP RUN. ``` -------------------------------- ### CJBuild DLL Path Example (PDS) Source: https://www.ibm.com/docs/en/cobol-zos/6.4.0?topic=java-cjbuild-command-reference Example of specifying a PDS dataset name for the DLL output directory. ```text // ``` -------------------------------- ### Example Package Name Source: https://www.ibm.com/docs/en/cobol-zos/6.4.0?topic=java-cjbuild-command-reference This example demonstrates how to specify a package name for native methods. Three classes will be created under this package. ```bash com.acme.COBOL.myapp1 ``` -------------------------------- ### CJBuild Program File Example Source: https://www.ibm.com/docs/en/cobol-zos/6.4.0?topic=java-cjbuild-command-reference An example of the content for the program file argument, listing COBOL programs for processing. ```text COBPROG1 COBPROG2 ``` -------------------------------- ### Example String with Supplementary Code Point Source: https://www.ibm.com/docs/en/cobol-zos/6.4.0?topic=pu8duu1ndt-using-intrinsic-functions-process-utf-8-encoded-data This is an example of a UTF-8 string containing a supplementary character, indicated by a sequence starting with x'F0'. ```Hexadecimal x'6162D0B0E4BA8CF0908C826364' ``` -------------------------------- ### XML Encoding Declaration Example Source: https://www.ibm.com/docs/en/cobol-zos/6.4.0?topic=encoding-specifying This example shows how to specify an encoding declaration within the XML declaration of an XML document. The declaration must start at the first byte of the document. ```xml ``` -------------------------------- ### COBOL Program Identification Division Example Source: https://www.ibm.com/docs/en/cobol-zos/6.4.0?topic=program-identifying This snippet demonstrates the structure of the IDENTIFICATION DIVISION, including program name, author, installation, and dates. The DATE-COMPILED field is automatically updated. ```COBOL IDENTIFICATION DIVISION. Program-ID. Helloprog. Author. A. Programmer. Installation. Computing Laboratories. Date-Written. 09/04/2019. Date-Compiled. 09/08/2019. ``` -------------------------------- ### Set JVM Initialization Options for COBOL Source: https://www.ibm.com/docs/en/cobol-zos/6.4.0?topic=unix-running-oo-applications-that-start-cobol-program Use the COBJVMINITOPTIONS environment variable to specify JVM startup options. Separate multiple options with blanks. This example sets initial and maximum heap sizes and enables verbose garbage collection logging. ```bash export COBJVMINITOPTIONS="-Xms10000000 -Xmx20000000 -verbose:gc" ``` -------------------------------- ### Sample cob2 -M Output Source: https://www.ibm.com/docs/en/cobol-zos/6.4.0?topic=command-cob2-syntax-options This sample output demonstrates the format of a make dependency file generated by the -M option, listing source files and their included copybook dependencies. ```text mkdepend.o: mkdepend.cbl mkdepend.o: /home/usera/test/f171592/WSCOPY1.cpy mkdepend.o: /home/usera/test/f171592/PDCOPY1.cpy mkdepend.o: /home/usera/test/f171592/PDCOPY2.cpy ``` -------------------------------- ### Environment Variable Setup for COPY Statement (z/OS UNIX Shell) Source: https://www.ibm.com/docs/en/cobol-zos/6.4.0?topic=program-compiler-directing-statements Example of setting environment variables MYCOPY and MYLIB to specify copybook locations when compiling in the z/OS UNIX shell. ```Shell export MYCOPY=mystuff/today.cpy export MYLIB=/u/user1 ``` -------------------------------- ### Instance Method with Single Parameter Source: https://www.ibm.com/docs/en/cobol-zos/6.4.0?topic=method-overloading-instance Defines an instance method 'init' that accepts a single parameter for initializing an Account instance with a default balance. ```COBOL Linkage section. 01 inAccountNumber pic S9(9) binary. Procedure Division using by value inAccountNumber. ``` -------------------------------- ### Example of a Malformed XML Document Source: https://www.ibm.com/docs/en/cobol-zos/6.4.0?topic=input-handling-xml-parse-exceptions This XML document is not well-formed because the element end tag 'mmsg' does not match the element start tag 'msg'. This illustrates a common scenario leading to an XML PARSE exception. ```xml Hello ``` -------------------------------- ### Initialize input/output buffers and pointers for zlib Source: https://www.ibm.com/docs/en/cobol-zos/6.4.0?topic=processing-using-zlib-compression-from-cobol-program This snippet demonstrates initializing the available input and output buffer sizes, total input count, and setting the next input and output pointers for the z_stream structure. This prepares the stream for data compression. ```COBOL ***************************************************************** * Initialize available input, output, total in for deflate * ***************************************************************** Compute avail_in of z = 65536 Compute avail_out of z = 65536 Compute total_in of z = 0 ***************************************************************** * Set input and output pointers * ***************************************************************** Set next_out of z to Address of zoutput Set next_in of z to Address of zinput ``` -------------------------------- ### Alphanumeric Function Max and Length Example Source: https://www.ibm.com/docs/en/cobol-zos/6.4.0?topic=item-returning-variable-results-alphanumeric-national-functions Demonstrates `Function Max` to find the larger of two alphanumeric strings and `Function Length` to get its length. The output to `R3` and `L` depends on the comparison of `R1` and `R2`. ```COBOL 01 R1 Pic x(10) value "e". 01 R2 Pic x(05) value "f". 01 R3 Pic x(20) value spaces. 01 L Pic 99. . . . Move Function Max(R1 R2) to R3 Compute L = Function Length(Function Max(R1 R2)) ``` -------------------------------- ### Build Output Example Source: https://www.ibm.com/docs/en/cobol-zos/6.4.0?topic=options-dll The output shows the compilation progress and the execution flow of PROG1 calling SUB1. It confirms successful compilation and dynamic call execution. ```text PP 5655-EC6 IBM Enterprise COBOL for z/OS 6.4.0 in progress ... End of compilation 1, program SUB1, no statements flagged. PP 5655-EC6 IBM Enterprise COBOL for z/OS 6.4.0 in progress ... End of compilation 1, program PROG1, no statements flagged. Entered PROG1 Calling DLL routine SUB1 from PROG1 Entered DLL routine SUB1 Exited DLL routine SUB1 Exited PROG1 ``` -------------------------------- ### COBOL STRING Statement Example Source: https://www.ibm.com/docs/en/cobol-zos/6.4.0?topic=string-example-statement Constructs a report line using the STRING statement. It moves data from various fields into RPT-LINE, starting at a specific position indicated by LINE-POS. Data is delimited by size or by a decimal point. ```cobol STRING LINE-NO SPACE CUST-INFO INV-NO SPACE DATE-DUE SPACE DELIMITED BY SIZE BAL-DUE DELIMITED BY DEC-POINT INTO RPT-LINE WITH POINTER LINE-POS. ``` -------------------------------- ### Reverse String using MOVE Function Source: https://www.ibm.com/docs/en/cobol-zos/6.4.0?topic=functions-transforming-reverse-order-reverse Use the MOVE function REVERSE to reverse the order of characters in a variable. For example, if the starting value is 'JOHNSON_b__b__b_', the value after the statement is performed is '_b__b__b_NOSNHOJ', where '_b_' represents a blank space. ```COBOL Move Function Reverse(Orig-cust-name) To Orig-cust-name ``` -------------------------------- ### CJBuild DLL Path Example (Specific PDS) Source: https://www.ibm.com/docs/en/cobol-zos/6.4.0?topic=java-cjbuild-command-reference Example of specifying a specific PDS dataset name for the DLL output directory. ```text //COBOL.LOAD or //'USER.COBOL.LOAD' ``` -------------------------------- ### Coding a Get Method in COBOL Source: https://www.ibm.com/docs/en/cobol-zos/6.4.0?topic=method-coding-attribute-get-set-methods This example defines an instance method `getBalance` within the `Account` class to return the value of the `AccountBalance` instance variable. Both the method and the variable are declared within the `OBJECT` paragraph of the class definition. ```COBOL OBJECT. getBalance. MOVE AccountBalance TO RETURN-CODE. GOBACK. getBalance END-METHOD. AccountBalance PIC 9(9) VALUE 0. OBJECT END. ``` -------------------------------- ### Example: Checking VSAM Status Codes Source: https://www.ibm.com/docs/en/cobol-zos/6.4.0?topic=errors-handling-in-input-output-operations Reads an indexed file starting from the fifth record, checks the file status key after each I/O request, and displays VSAM status codes when the file status key indicates an error. ```COBOL 01 FILE-STATUS-KEY PIC XX. 01 VSAM-STATUS-CODE PIC X(4). PROCEDURE DIVISION. MAIN-LOGIC. OPEN INPUT MY-VSAM-FILE. IF FILE-STATUS-KEY NOT = '00' DISPLAY "Error opening VSAM file. Status: " FILE-STATUS-KEY STOP RUN END-IF. MOVE 5 TO RECORD-NUMBER. READ MY-VSAM-FILE NEXT RECORD AT END DISPLAY "End of file reached before starting at record 5." GO TO END-PROGRAM. NOT AT END IF FILE-STATUS-KEY NOT = '00' DISPLAY "Error reading initial record. Status: " FILE-STATUS-KEY GO TO END-PROGRAM END-IF END-READ. PERFORM VARYING RECORD-NUMBER FROM 6 BY 1 UNTIL RECORD-NUMBER > 10 READ MY-VSAM-FILE NEXT RECORD INVALID KEY DISPLAY "INVALID KEY error on read. Status: " FILE-STATUS-KEY MOVE FILE-STATUS-KEY TO VSAM-STATUS-CODE DISPLAY "VSAM Status Code: " VSAM-STATUS-CODE GO TO END-PROGRAM NOT AT END IF FILE-STATUS-KEY NOT = '00' DISPLAY "Error reading record " RECORD-NUMBER ". Status: " FILE-STATUS-KEY MOVE FILE-STATUS-KEY TO VSAM-STATUS-CODE DISPLAY "VSAM Status Code: " VSAM-STATUS-CODE GO TO END-PROGRAM END-IF DISPLAY "Record read: " RECORD-DATA END-READ END-PERFORM. END-PROGRAM. CLOSE MY-VSAM-FILE. STOP RUN. ``` -------------------------------- ### Example: Checking File Status Key After Opening a File Source: https://www.ibm.com/docs/en/cobol-zos/6.4.0?topic=errors-handling-in-input-output-operations Demonstrates a basic check of the file status key immediately after opening a file to verify the operation's success. ```COBOL 01 FILE-STATUS-KEY PIC XX. PROCEDURE DIVISION. MAIN-LOGIC. OPEN INPUT MY-FILE. IF FILE-STATUS-KEY NOT = '00' DISPLAY "Error opening file. Status: " FILE-STATUS-KEY STOP RUN END-IF. * ... rest of your program ... CLOSE MY-FILE. STOP RUN. ``` -------------------------------- ### COBOL National Literal Continuation Example Source: https://www.ibm.com/docs/en/cobol-zos/6.4.0?topic=b-continuation-lines Shows a continued national literal (prefixed with N). Spaces between the starting and ending quotation marks on the continued line are counted. The literal is 60 national character positions long (120 bytes). ```COBOL |...+.*..1....+....2....+....3....+....4....+....5....+....6....+....7.. 000003 N"AAAAAAAAAABBBBBBBBBBCCCCCCCCCCDDDDDDDDDDEEEEEEEEEE - "GGGGGGGGGG" ``` -------------------------------- ### Example cjbuild Linking Command Source: https://www.ibm.com/docs/en/cobol-zos/6.4.0?topic=java-troubleshooting This is an example of a linking command used by the cjbuild utility, showing the compilation of object files and linking with Java libraries. ```shell # Linking native method DLL /home/userid/cobol/igyv6r4/bin/cob2 -bdll -o //COBOL.LOAD(libapp1) COBPROD_java_native_app1.o COBPROD_java_native_WS_app1.o addlist_java_native_app1.o Java.CobProd.printResult.o /home/userid/jtoc/demo2/java_new5/J8.0_64/bin/j9vm/libjvm.x /home/userid/cobol/igyv6r4/lib/igzcjni2.x ``` -------------------------------- ### COBOL 6: Syntax Checked VALUE Clause in LINKAGE SECTION Source: https://www.ibm.com/docs/en/cobol-zos/6.4.0?topic=cecv6-cobol-source-code-differences-in-enterprise-cobol-version-6 Starting with Enterprise COBOL 6.1, the VALUE clause for LINKAGE SECTION items is syntax checked. This example shows the same code as above, but with COBOL 6's error message indicating incompatibility. ```cobol 000224 LINKAGE SECTION. 000225 01 ALPH-ITEM PIC X(4) VALUE 1234. ==000225==> IGYGR1080-S A "VALUE" clause literal was not compatible with the data category of the subject data item. The "VALUE" clause was discarded. ``` -------------------------------- ### Instance Method with Two Parameters Source: https://www.ibm.com/docs/en/cobol-zos/6.4.0?topic=method-overloading-instance Defines an overloaded instance method 'init' that accepts two parameters, allowing initialization with a specified account number and balance. ```COBOL Linkage section. 01 inAccountNumber pic S9(9) binary. 01 inBalance pic S9(9) binary. Procedure Division using by value inAccountNumber inBalance. ``` -------------------------------- ### COBOL Program to Initialize Product Information Source: https://www.ibm.com/docs/en/cobol-zos/6.4.0?topic=java-example-cobprod-application-building-running The cobprod.cbl program initializes a WORKING-STORAGE section with product information, making it accessible to Java. This is for demonstration purposes; data should not typically be hardcoded. ```cobol ********************************************************************* * * * IBM Enterprise COBOL for z/OS * * Version 6 Release 4 Modification 0 * * * * LICENSED MATERIALS - PROPERTY OF IBM. * * * * 5655-EC6 COPYRIGHT IBM CORP. 2022, 2024 * * * * US GOVERNMENT USERS RESTRICTED RIGHTS - USE, * * DUPLICATION OR DISCLOSURE RESTRICTED BY GSA * * ADP SCHEDULE CONTRACT WITH IBM CORP. * * * ********************************************************************* * cobprod initializes 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 * NOTE: This is for demonstration purposes only. * Data should NOT be placed in your COBOL program code. 01 prod-list-size pic s9(9) comp-5 value 5. 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. * Make this table directly accessible to Java >>java-shareable on 01 prod-info redefines prod-info-data. 03 prod-list occurs 5 times. 05 prod-name pic x(20). ``` -------------------------------- ### COBOL Example: Processing a Java Integer Array Source: https://www.ibm.com/docs/en/cobol-zos/6.4.0?topic=arrays-example-processing-java-integer-array This COBOL code demonstrates how to use JNI services to access and process elements of a Java integer array. It includes setting up JNI environment, retrieving array length, getting array elements, and releasing them. ```COBOL cbl thread,dll Identification division. Class-id. OOARRAY inherits Base. Environment division. Configuration section. Repository. Class Base is "java.lang.Object" Class jintArray is "jintArray". Identification division. Object. Procedure division. Identification division. Method-id. "ProcessArray". Data Division. Local-storage section. 01 intArrayPtr pointer. 01 intArrayLen pic S9(9) comp-5. Linkage section. COPY JNI. 01 inIntArrayObj usage object reference jintArray. 01 intArrayGroup. 02 X pic S9(9) comp-5 occurs 1 to 1000 times depending on intArrayLen. Procedure division using by value inIntArrayObj. Set address of JNIEnv to JNIEnvPtr Set address of JNINativeInterface to JNIEnv Call GetArrayLength using by value JNIEnvPtr inIntArrayObj returning intArrayLen Call GetIntArrayElements using by value JNIEnvPtr inIntArrayObj 0 returning IntArrayPtr Set address of intArrayGroup to intArrayPtr * . . . process the array elements X(I) . . . Call ReleaseIntArrayElements using by value JNIEnvPtr inIntArrayObj intArrayPtr 0. End method "ProcessArray". End Object. End class OOARRAY. ``` -------------------------------- ### Example JSON Input Source: https://www.ibm.com/docs/en/cobol-zos/6.4.0?topic=input-parsing-json-documents This is an example of a JSON string that can be parsed by the COBOL program. ```JSON {"msg":{"ver":5,"uid":1000,"txt":"Hello World!"}} ``` -------------------------------- ### XML Output Example Source: https://www.ibm.com/docs/en/cobol-zos/6.4.0?topic=output-example-generating-xml This XML snippet represents a sample purchase order output from program XGFX. It includes details such as order date, shipping and billing information, and item specifics. ```xml ``` -------------------------------- ### Specifying Entry Point with cob2 Source: https://www.ibm.com/docs/en/cobol-zos/6.4.0?topic=command-cob2-syntax-options The -e xxx option specifies the program to be used as the entry point for the module. If not specified, the first program or object file listed is used as the default entry point. ```text -e xxx ```