More‎ > ‎

COBOL (z/OS) Survival Guide for Java Developers - Part 1

BY MARKUS SPRUNCK


You like to talk with mainframe people without making yourself look stupid. So, then this guide provides more than helpful context information for you. I'm sure this guide will save you a lot of time and energy for getting started with COBOL development on z/OS®. 

All the mainframe technologies are not difficult to understand, it is just a lot information about very “mature” technologies. The purpose of this article is to give experienced Java developers some context information to save time. It is part one is the start of a series of article, but it covers already the basics. 

Motivation

The term mature technologies is surly a euphemism. Some of the stuff is more than 50 years old and just understandable in the context of punch cards (see https://en.wikipedia.org/wiki/Punched_card) and extremely limited hardware resources. 

The receipt of Steak Wellington is even older, but it tastes still delicious provided you are not vegetarian. So, don’t be discouraged just because the technology is old. 

These days, almost no developer starts just for fun with the development on mainframe. My personal motivation was a new project with a significant amount of code developed on z/OS®. In my role as IT Architect it is just mandatory to know at least basics about all technologies used in a large project, so I started to lean mainframe.

But also for young developers and students it makes sense to spend time with mainframe. It will survive decades from now, because there is just too much legacy code in the large enterprises and institutions.

What you shouldn’t expect?

This is not a tutorial — there are good resources in the web, e.g., https://www.tutorialspoint.com/cobol/index.htm.  This survival guide written for experienced Java developers. That implies there is no discussion about Java basics. 

Object-oriented COBOL is not in the focus, because in practice you will not find it quite often - even in new applications. COBOL expects a strict format of the source code. This will not be discussed in the following.

First things first - Hello World in COBOL

The following listing shows the main structure which is common to all COBOL programs. It is an almost minimal batch program. 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
      *
      * First Hello World program UATMSP00
      *

       IDENTIFICATION  DIVISION.
       PROGRAM-ID. UATMSP00.

       ENVIRONMENT DIVISION.

       DATA DIVISION.

       WORKING-STORAGE SECTION.
       01  WS-INITIAL-HELLO.
           02 MY-MESSAGE  PIC  X(30) VALUE 'HELLO, WORLD!'.

       PROCEDURE DIVISION.
       MY-MAIN-PROCEDURE.

           DISPLAY '+++ UATMSP00 +++ START'
           DISPLAY WS-INITIAL-HELLO

           MOVE 'HELLO, MARKUS!' TO WS-INITIAL-HELLO
           DISPLAY MY-MESSAGE

           DISPLAY '+++ UATMSP00 +++ END '
           STOP RUN.

A short explanation of the COBOL code:
  • Line 13: In the WORKING-STORAGE SECTION the variable WS-INITIAL-HELLO is defined with the initial value ‘HELLO, WORLD!’.

    Remark: 
    When you put this data structure definition into an external file with the name extension .cpy is a so called copybook file. A copybook can be shared between different programs. This is not the same as an import in Java it is more like in C++ where a pre-processor includes sources. 

  • Line 14: PIC is the abbreviation of PICTURE and defines the data type. Here PIC X(30) defines structure which is thirty characters long. This variable can be directly used in the program, e.g. in DISPLAY or MOVE. 

  • Line 22: The Cobol verb MOVE copies a new value into the variable WS-INITIAL-HELLO.

  • Line 26: STOP RUN ends the execution of the program. Be careful with STOP RUN in subprograms it is a better idea to use GOBACK. 

  • Line 19: The command DISPLAY writes to the system output of the program. 
To run the COBOL batch a Job Control Language (JCL) File is needed.

Remark: JCL files can be compared with a starting script on Linux. A step is the called program or procedure. Up to 255 steps can be called JCL. The execution of singe steps can be controlled (skip of single steps or execution of exception handling).


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
//&SYSUIDT JOB (HI0000),'DEBUG TEST',
//             CLASS=L,MSGLEVEL=1,MSGCLASS=H,NOTIFY=&SYSUID
//*
//* INCLUDE MEMBER=QEJLIB
//JOBLIB    DD DISP=SHR,DSN=QE.LOD0.GRA.EXECLIB
//*
//********************************************************************
//* Start the COBOL program UATMSP00
//********************************************************************
//*
//KRAWALL EXEC PGM=UATMSP00
//SYSUDUMP DD  SYSOUT=*
//SYSOUT   DD  SYSOUT=*
//SYSOUX   DD  SYSOUT=*
//*


  • Line 01: Identifies the job card and does some additional information how this batch job should be executed, name of the job, etc. 

  • Line 05: A definition of a JOBLIB simplifies (speeds up) the search for the compiled COBOL program. 

    Remark: 
    This is a data definition (DD) statement which defines a data set that a program uses when it runs. There must be a DD statement for all used data sets, e.g., input files, output files. 

  • Line 11: Here the program with the name UATMSP00 is executed (see EXEC). This step is called KRAWALL (you could translate it with the term noise) which has no further meaning, it is just a name to identify the output and the step itself. 
The expected output of this first Hello World in COBOL is: 

1
2
3
4
+++ UATMSP00 +++ START
HELLO, WORLD!
HELLO, MARKUS!
+++ UATMSP00 +++ END  

Now the task is to write a program in Java with the same output. 

Let’s see how this would look like in Java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public class UatMsp00 {

    private static String myMessage = "HELLO, WORLD!";

    public static void main(String[] args) {
        System.out.println("+++ UATMSP00 +++ START");
        System.out.println(myMessage);

        myMessage = "HELLO, MARKUS!";
        System.out.println(myMessage);

        System.out.println("+++ UATMSP00 +++ END");
    }
}

This simple Hello World (Java) needs no special explanation, but a deeper comparison between COBOL and Java is interesting.

Now its time to go more into detail



Example


Comment


COBOL:

01
MY-MESSAGE  PIC X(30) 

    VALUE 'HELLO'.



JAVA:

private static String
myMessage 

    = "HELLO";



The COBOL structure represents a kind of character array. This is not the same as the Java String Object, but here nobody would use char[30].

COBOL is not typed like Java. The concept of COBOL is a kind of “declaration by example/pattern”. COBOL knows only basic data types, i.e. numbers and alphabetic/special characters. The definition works with PICTURES (PIC). This is a kind of pattern for more complex data definitions:

o numeric (9)
o alphabetic (A)
o alphanumeric (X)
o implicit decimal (V)
o assumed Decimal (P)
o sign (S)

So the PIC X(30) means that the data structure has exactly 30 alphanumeric characters.

These pattern are also used to print formatted values (very nice functionality).


COBOL:

DISPLAY
MY-MESSAGE

JAVA:

System.out.println(
myMessage
);


Like in Java the direct output to the System out is discouraged.
In COBOL you may put the character d in column 7 of the source file.
This enables the code just when compiled with the debug flag.


COBOL:
MOVE
'HELLO, MARKUS!' TO MY-MESSAGE

JAVA:

myMessage
= "HELLO, MARKUS!";


COBOL verb MOVE is not an assignment. It moves data into a data structure. In the case the target variable has a different size not all parts will be overwritten or truncated.

There are some rules what you can move into a data structure but there are not a lot of limitations.


COBOL:

MOVE
8 TO RETURN-CODE

STOP RUN

JAVA:

System.exit(
8)




In both situations the return code zero means no problems. In COBOL the return code has to be set (default is 0) in the case there is a problem.

In the JCL the return code can be tested and if needed a different job order executed.


COBOL:
IDENTIFICATION DIVISION.

       PROGRAM-ID. UATMSP00.




JAVA:
public class
UatMsp00 {


...


}


Compared with the structure of Java a single COBOL program is like a Java Class.

All data structures (variables) are global within the scope of the COBOL program (like attributes in a Java class)

To better manage large COBOL

To better manage large COBOL programs you create separated applications you create separated programs with special concerns.


COBOL:

PROCEDURE DIVISION.

       MY-MAIN-PROCEDURE.



JAVA:

public static void
main(String[] args) {

   

...


}



The Program execution starts from PROCEDURE DIVISION.
A section can be the subject of a PERFORM statement (like Java method call).

Some Cobol basics

Complete hierarchy 

 
Program
  • single file (maybe additional files with copybooks) 
  • smallest compile and deploy unit
  • call by other programs with parameter possible

Division
  • identifies the principal main components of the program 
  • four fixed names
 
Section
  • name followed by the word SECTION followed by a full stop
  • all paragraphs in the section will be performed sequentially until the start of the next section
 
Paragraph
  • paragraph is a block of code made up of one or more sentences 
  • name followed by followed by a full stop
  • performing will cause only that paragraph to be performed

 Sentence
  • consists of one or more statements and is terminated by a full stop
  • do not use NEXT SENTENCE any more

 Statement
  • starts with a COBOL verb and an operand or operands
  • some basic verbs are ACCEPT, DISPLAY, INITIALISE, MOVE, ADD, SUBTRACT, MULTIPLY, DIVIDE are COMPUTE

Every COBOL program has the four divisions


IDENTIFICATION DIVISION
(mandatory) 

  • here you find the name of the program (mandatory) and 
  • other (optional) information, like Author, Date-Written

ENVIRONMENT DIVISION 
(optional)

  • all aspect which are dependent on the computing environment
 
DATA DIVISION (optional) 
  • all information about data this division
    • FILE SECTION (optional)
      data used for I/O
    • LINKAGE SECTION (optional)
      describes data from another program.
    • WORKING-STORAGE SECTION (optional) 
      statically allocated storage (remains for the life of the run unit)
    • LOCAL-STORAGE SECTION (optional) 
      storage allocated each time a program is called (automatically allocated at the end of the program)
    • LINKAGE SECTION (optional)
      describes data from another program.

PROCEDURE 
DIVISION 
(mandatory)

  •  here is all the functionality

Job Control Language (JCL) basics

Java programs can be started as standalone processes from the command line or they run in a container application. On mainframe there are also standalone running programs (batch).

But JCL makes more than just starting a single program like in the Hello World example. It is not unusual to have hundreds thousand batch jobs running on a mainframe.

Main Functionality:
  • Order of program executions depending on conditions and return codes
  • Definition of input and output 
  • Binding of Logical and Physical resources 
  • Call of special programs optimised for fast data management, like sort, merge or filtering

Further reading

A comment on trademarks

In the text you find a lot of terms which are trademarks (see https://www.ibm.com/legal/us/en/copytrade.shtml). To improve the readability not all are marked in the text.

Sponsored Link