Calling OO COBOL from Java

This chapter describes how you can access COBOL objects from Java programs. This is slightly different from the method for accessing procedural COBOL from Java programs (see the chapter Calling Procedural COBOL from Java for information about this).

In this Chapter

Overview

You can write classes in OO COBOL which can be called from Java programs as though they were Java classes. You do this by providing a Java wrapper class, which provides a function for each method in the OO COBOL class. If you have Net Express, the Class and Method Wizards make this easy for you, by generating the Java code at the same time as the COBOL code. You can then publish the resulting Java classes to your UNIX system.

The functions in the Java wrapper class put all the parameters for the method into a Java array, and then call one of the member functions of Java class com.microfocus.cobol.RuntimeSystem to invoke the method in the OO COBOL class and return the result. This is shown in Figure 0-1 below:

Calling OO COBOL from Java

Figure 1: Calling OO COBOL from Java

The rest of this chapter explains how you write the OO COBOL class and its Java wrapper.

You need to have at least a basic knowledge of the Java language to be able to use this technology effectively. Sun's Java site is a good starting place.

Before You Start

Before you start, you need to set up your environment so that the COBOL and Java run-time systems can interact. See the section Setting Up the Environment for Java and COBOL in the chapter Using Java and COBOL Together.

Writing a Java Class in OO COBOL

To write a class in OO COBOL which can be called from Java, you need to do the following:

  1. Set the following directive in your program:
    $set ooctrl"+p-f"
  2. Ensure that the OO COBOL class inherits from javabase.
  3. Create a Java wrapper class for the OO COBOL class. The wrapper class must have the same class name as the filename of the OO COBOL class.
  4. For each method that you create in your OO COBOL class, create a corresponding method in the Java wrapper class.
  5. Package the class inside a .dll file when deploying it on Windows platforms.

    You can also use classes compiled to .int or .gnt, but this does not allow you to package several classes inside one file.

Importing the COBOL Support

The support needed for the Java wrapper class to communicate with the OO COBOL class is in the com.microfocus.cobol package. The wrapper must include the following statement:

import com.microfocus.cobol.* ;

You must also ensure mfcobol.jar is on the Java classpath, or your Java programs will not compile or run. See the section Setting Up the Environment for Java and COBOL in the chapter Using Java and COBOL Together.

The Wrapper Class

The Java wrapper class must extend either microfocus.cobol.RuntimeObject or microfocus.cobol.RuntimeSystem. This affects the lifetime of the OO COBOL instances represented by instances of the Java wrapper class:

The wrapper class needs to be initialized with the library and filename of the COBOL class it is wrapping. You do this by including the following code inside the Java wrapper class:

static 
{
  cobloadclass("libname","filename","fullJavaClassName");  
}

where:

libname is the name of the .dll file which contains the OO COBOL class. You can leave this parameter as null if the class is not packaged inside a .dll file - for example, if it is running as .int or .gnt code.
filename is the filename of the OO COBOL class.
FullJavaClassName is the Java classname corresponding to the OO COBOL class.

There are three cobloadclass() methods, which provide slightly different ways of identifying the library, COBOL file and the Java wrapper class.

Adding and Removing Methods

Every method you add to the COBOL class must have a corresponding function in the Java wrapper class.

The Java function must do the following:

  1. Declare the exceptions which can be thrown from an invoke of a COBOL method.

    By default, these are Exception (thrown if you raise an exception in COBOL) and CobolException (thrown by the COBOL run-time system). If your class is to be deployed either as an Enterprise JavaBean, or using Java Remote Method Invocation (RMI), you should also add RemoteException.

  2. Construct a Java array containing the parameters to be passed from Java to the COBOL method.

    Parameters are passed from Java to the COBOL run-time system in a Java array.

  3. Invoke one of the cobinvoke_ or cobinvokestatic_ methods provided by the class RuntimeSystem.java.

    There is a cobinvoke_ and cobinvokestatic_ method corresponding to each possible return type from a Java function (for example, cobinvoke_int returns an int). Use the cobinvoke_ methods for invoking OO COBOL instances. Use the cobinvokestatic_ methods for invoking OO COBOL classes.

    See the Java Run-time Class Library ReferenceJava Run-time Class Library Reference, which is in docs/mfcobol.docs.zip, for the full list of cobinvoke_ functions.

    You need to determine which Java data type maps to the return type from the COBOL method, and then choose the appropriate cobinvoke_ function. See the chapter Java Data Types for more information.

OO COBOL factory methods are mapped onto static functions in the Java wrapper.

The two code samples below show a COBOL instance method, and the corresponding function in the Java wrapper. This is the COBOL method.

 method-id. "myMethod".
 local-storage section.
*>---USER-CODE. Add any local storage items needed below.
 linkage Section.
 01 myParameter            pic x(4) comp-5.
 01 myReturnValue          pic x(4) comp-5.
 procedure division using by reference myParameter
                    returning myReturnValue.
*>---USER-CODE. Add method implementation below.
     exit method. 
end method "myMethod".

This is the Java wrapper method.

public int myMethod (Integer myParameter) throws Exception, 
                          CobolException, RemoteException 
{
    // Parameters are passed to COBOL in an array
    Object[] params = {myParameter};
    return ((int) cobinvoke_int ("myMethod", params)); }

Although the name of a method is usually the same for the Java wrapper class and the COBOL class, it does not have to be. This enables you to implement method overloading in the Java wrapper.

Method overloading enables you to have several methods with the same name, but which take different types or numbers of parameters. Java supports method overloading, but COBOL does not. But you can add overloaded functions to the Java wrapper, and have them call differently named methods in the COBOL class.

For example, you could have these overloaded functions in your Java wrapper class:

public int add (int a, int b) 
{...}
public int add (int a, int b, int c)
{...}

And map them to methods "add2" and "add3" in your COBOL class.

Throwing Exceptions from COBOL

You can throw a Java exception from your COBOL code. The exception object can be any Java class. The methods for throwing exceptions are provided in the javasup class, which is documented in your Java Run-time Class Library Reference, which is in docs/mfcobol.docs.zip.

You can use either of the following methods:

invoke javasup "throwException" using aCOBOLProxy

where:

aCOBOLProxy is a COBOL proxy to any Java object of type Throwable.

or:

invoke javasup "throwNewException" using 
	              javaclassnamedescription

where:

javaclassname is the name of the class of Java exception to throw.
description is a text description of the exception.

Using BY REFERENCE Parameters

When parameters to a method are declared as BY REFERENCE, the default behavior is for any updates to those parameters to be reflected in the corresponding Java objects in the calling Java class on return from the OO COBOL method.

If you do not want the Java objects to be updated, you can set the following static variable:

static boolean mfcobol.runtimeProperties.updateByRefParams;

The default value for this variable is true. Set it to false if you do not want the Java objects to be updated.

Your COBOL development system includes a demonstration program that illustrates the different ways of handling BY REFERENCE parameters. See the section Demonstration Programs.

Working with Java Programs

When you develop mixed COBOL and Java programs, you need to be aware of the following:

Demonstration Programs

Your COBOL development system includes some demonstration programs that illustrate different aspects of calling OO COBOL from Java. These demonstration programs are in the $COBDIR/demo/javademo/cobol directory in your product installation. Each directory includes all relevant program files, as well as a readme.txt file to explain the programs in more detail.

The following table shows the directories containing the demonstration programs and gives a brief description of the purpose of the demonstration:

Directory Illustrates
array Receiving and accessing a Java array in OO COBOL.

Creating a Java array in OO COBOL and passing it to Java.

record Writing a simple COBOL Java object.

Receiving structures in OO COBOL methods from Java objects that implement the DataType interface.

Passing parameters BY REFERENCE.

simple Writing a simple COBOL Java object.

Calling both class and instance OO COBOL methods using Java.

sort Writing a simple COBOL Java object.

Calling OO COBOL methods using Java.

Calling Java methods using OO COBOL.

Dealing with Java objects as parameters and return values.