Take more control of your applications with innovative COBOL and Java technology

How do you take your Java?

A little dash or a splash of COBOL can make for the perfect mix

isCOBOL allows programmers to take advantage of the best of both COBOL and Java by providing common ground between the two technologies. With isCOBOL, organizations can add the power and flexibility of the Java platform to existing COBOL programs and also make COBOL investments easily accessible to the Java world. This article briefly examines some of the synergies that exist between COBOL and Java technology and includes a few examples to illustrate how COBOL and Java programmers can incorporate each others technologies with the isCOBOL Application Platform Suite.

COmmon Business Oriented Language – 50 Years Young

One of the original design goals for COBOL was to offer a programming language that would be easy to read and understand by managers with no programming training. Although managers rarely took advantage of that -- either fifty years ago or today -- a direct outcome of this goal is that the COBOL language is comprised of familiar English building block structures such as verbs, clauses, sentences, sections, and divisions. The Environment Division, for instance, is a set area found in all COBOL programs that describes the particular environment for which the program is written and all external references, such as to devices, files, currency symbols, and object classes that a COBOL application requires. A COBOL developer looking to move a program to a new platform or region knows that this centralized area of code will have to be reviewed and potentially altered to accommodate the new target platform’s requirements.

COBOL’s straightforward programming approach, clear organizational structure, ability to deal with business logic, adoption of latest technological paradigms, and portability are some of the primary factors that contributed to its widespread adoption and persistence in the industry.

Originally called Green, then changed to Oak, finally known as Java – 15 years mature

The Java programming language leverages small, modular building blocks (known as object classes) that are tied together to form a complete application. Two important concepts in object-oriented programming are encapsulation and inheritance:

  • Encapsulation means that a Java class can be viewed as a "black box" whose internal workings are hidden. To use a class it is only necessary to know what the class does, not the details of how the class does it. As long as the public interface stays the same, the internal mechanisms of a class can be improved or the class can be replaced with a different one without impact on other components.
  • Inheritance means that new classes can be formed using classes that have already been defined, leveraging and extending their functionality.

Object classes can be easily assembled, disassembled, and reused in new applications as business requirements change. Since Java programs run in any environment that supports the proper Java Runtime Environment (JRE) they are extremely portable and can be moved between platforms without any program code changes.

It is this modularity, portability, and reusability that contribute to the growing adoption of Java programming in the industry today.

COBOL with a dash of Java

With isCOBOL APS, developers can quickly add a wealth of new functionality to COBOL applications by integrating free, reusable, modular solutions found in the Java SE and Java EE Development Kits as well as in various open source communities such as The Apache Software Foundation and SourceForge. Solutions available to Java developers are now equally available to COBOL developers -- the challenge is only to identify the existing Java solution needed for a particular programming task and to write the COBOL code to interface with it.

It is important to emphasize that to integrate these Java solutions, a COBOL programmer does not need to be trained and experienced in the Java language. The programmer’s only requirements are to be able to understand basic object-oriented concepts such as classes and methods, to know how to create an object and invoke a method in COBOL, and to understand how to read and use the Javadocs for the particular Java class to be used. These topics are covered in the isCOBOL APS Language Reference Manual and any beginner's book or online resource on Java programming.

As an example of the new functionality isCOBOL makes available to the COBOL developer, consider the full array of XML APIs and tools available to a Java programmer. To parse local XML files or XML from a URL, a COBOL developer could use the JDOM Java package. Here is a simple program that retrieves an XML document from a URL and parses the XML using JDOM.

program-id. XMLfromURL.
                                   configuration section.
                                       class J_Iterator      as "java.util.Iterator"
                                       class J_Element       as "org.jdom.Element"
                                       class J_SAXBuilder    as "org.jdom.input.SAXBuilder"
                                       class J_Document      as "org.jdom.Document"
                                       class J_URL           as ""

                                   working-storage section.
                                   77  W_SAXBuilder                object reference J_SAXBuilder.
                                   77  W_Document                  object reference J_Document.
                                   77  W_Element                   object reference J_Element.
                                   77  W_Iterator                  object reference J_Iterator.
                                   77  xml                         pic x any length.

                                   procedure division.
                                       move "file:///C:/Program%20Files/isCOBOL2008/sample
                                  -           "/issamples/files/Members.xml" to xml
                                     set W_SAXBuilder to J_SAXBuilder:: "new"
                                     set W_Document to W_SAXBuilder:: "build"(J_URL:: "new"(xml))
                                     set W_Element to W_Document:: "getRootElement"
                                     set W_Iterator to W_Element:: "getChildren":: "iterator"
                                         perform until not W_Iterator:: "hasNext"
                                             set W_Element to W_Iterator:: "next" as J_Element
                                             display W_Element:: "getChildText"( "first_name")
                                                         " " W_Element:: "getChildText"( "name")
                                                         " = " W_Element:: "getChildText"( "age")
                                       catch exception 
                                         display exception-object 

                                   stop run.

Developers can also take advantage of Java interfaces offered by third-party commercial software providers. For example, to add an electronic payment processing feature, a business could choose an electronic payment service provider and request the Java interface. The COBOL developer could use that Java interface directly from COBOL.

Java with a splash of COBOL

With isCOBOL APS, developers can now write programs in COBOL that can be called directly from Java as if they were written in the Java language. Java developers do not need to learn COBOL in order to make use of COBOL assets.

COBOL developers can present their subprograms as POJOs (Plain Old Java Objects). The Java developer receiving the objects needs only to know the class and method names, parameters, and return values to make use of them. The fact that the object class was written in COBOL or that it calls other COBOL subprograms is inconsequential.

The isCOBOL Compiler automatically converts existing COBOL programs into POJOs. No COBOL code changes are required. The COBOL program name becomes the Java class name. The resulting Java class has a "call" method which Java code can invoke to call the COBOL program. If required, the COBOL developer can precisely define the program's object interface using object-oriented COBOL syntax. This COBOL object can accept and return Java data types to make the Java developer's task even easier. For instance, if an organization is looking to integrate with a JEE server and needs to deploy a Servlet, Web Service, or Enterprise JavaBean (EJB), etc. those can all now be created natively in COBOL.

So what does this look like on the ground level to a developer? Let’s look at a few examples.

COBOL and Java Code Examples
Example 1 – COBOL calling Java

Here is the source code for a COBOL program that uses object-oriented COBOL syntax to access a Java class java.lang.System and use a Java data type java.lang.String.

PROGRAM-ID. obj-system.
                                   CONFIGURATION SECTION.
                                       class Sys as "java.lang.System"
                                       class JString as "java.lang.String"
                                   WORKING-STORAGE SECTION.
                                   77  pic-x-item  pic x(50).
                                  * There are two styles for specifying classes in object references 
                                   77  jstring1    object reference "java.lang.String".
                                   77  jstring2    object reference JString.
                                   PROCEDURE DIVISION.
                                  * There are 3 styles of invoking an object method 
                                  * Use the INVOKE statement 
                                       invoke Sys "getProperty" using "" giving pic-x-item.
                                       display "Operating System: " pic-x-item.
                                  * Use the SET statement with the double-colon operator 
                                       set jstring1 to Sys::"getProperty" ( "os.arch" ).
                                       display "OS Architecture: " jstring1.
                                  * Use the SET statement with the colon-greater-than operator 
                                       set jstring2 to Sys:>getProperty ( "java.version" ).
                                       display "Java Version: " jstring2.

This example demonstrates the use of the repository paragraph to define COBOL user-words that can be used to reference Java classes. It is also possible to specify the full Java class name when declaring a variable as an object reference. This example shows 3 styles of invoking methods. It shows the ability to use Java data types in COBOL statements taking advantage of the isCOBOL Framework to automatically convert the Java data type to the COBOL type that will work in that statement. This is just one example, any Java class and any Java data type can be used.

Example 2 - Java calling COBOL

COBOL developers can write programs in COBOL that can be called directly from Java as if they were written in the Java language. Here is the source code for an object class isobject written in COBOL.

                                   CLASS-ID. isobj as "isobject".

                                   IDENTIFICATION DIVISION.

                                   CONFIGURATION SECTION.
                                       class jint as "int".

                                   PROCEDURE DIVISION.

                                   IDENTIFICATION DIVISION.
                                   METHOD-ID. method1 as "add1".
                                   WORKING-STORAGE SECTION.
                                   77  var1     pic 9(9).
                                   77  var2     pic 9(9).
                                   77  result   object reference jint.
                                   LINKAGE SECTION.
                                   77  buffer object reference "java.lang.String".
                                   77  num    object reference jint.
                                   procedure division using buffer num returning result.
                                       display "1st parameter: " buffer.
                                       display "2nd parameter: " num.
                                       display "> isobject.add1: result = num + 1".
                                       set var1 to num.
                                       compute var2 = 1 + var1.
                                       set result to var2 as int.

                                   END METHOD.
                                   END FACTORY.

This class has one method "add1" which takes a Java string and a Java integer, displays them to the standard output stream, adds 1 to the integer, and returns the result. The isCOBOL Compiler will output a file named isobject.class which can be used by the Java developer.

And here is the source code for a Java program which uses the COBOL object isobject just as if it were written in Java language.

public class callCobolObject {
                            public static void main( String args[] ) {
                            String PAR1 = "BUFFER";
                            int PAR2 = 5;
                            System.out.println ("I'm calling method isobject.add1 with "+PAR1+", "+PAR2);
                            int RESULT = isobject.add1(PAR1, PAR2);
                            System.out.println ("I'm back with: "+RESULT);

Notice the call to isobject.add1(PAR1, PAR2) is simple Java code. The IDE used by the Java developer will assist with code completion. Even the IDE doesn't know that isobject.class was written in COBOL because behind-the-scenes the isCOBOL Compiler converted the COBOL to Java source code.

These are just a few examples of the collaboration and synergy that isCOBOL enables between the COBOL and Java worlds. Making your own perfect mix of COBOL and Java with isCOBOL should be …well, a piece of cake. Enjoy!

Click here to download zip file contains the 3 example COBOL programs mentioned in the article. View the README file for more information after

Learn more

To arrange code analysis report or to discuss your COBOL migration needs in more detail, email