PreviousCalling Java from COBOL Java Data TypesNext

Chapter 7: Calling Object COBOL from Java

This chapter describes how you can access COBOL objects from Java programs. This is slightly different to the method for accessing procedural COBOL from Java programs (see the chapter Calling Procedural COBOL from Java for more information about this). It also describes how you can create Enterprise Java Beans using COBOL.

7.1 Overview

You can write classes in Object 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 Object COBOL class. The Net Express class and method wizards make this easy for you, by generating the Java code at the same time as the COBOL code.

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 mfcobol.runtime to invoke the method in the Object COBOL class and return the result. This is shown in the diagram below:

image under construction

Fig 7-1: Path of a function call from Java to a method invoke in COBOL

The rest of this chapter explains how you write the Object COBOL class and its Java wrapper, and shows you the type of code created by the Net Express wizards. The last section describes the additional things you need to do if you want to make an Object COBOL class suitable for use as an Enterprise Java Bean (EJB).

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.

7.2 Before You Start

Before starting, you need to set up your environment so that the COBOL and Java run-time systems can interact. See Setting Up COBOL and Java in chapter Calling Java from COBOL for more information.

7.2.1 Unsupported JVM Errors

When you call COBOL from Java, the Object COBOL Java support loads one of several cbljvm_*.dll modules to interface to the Java Virtual Machine (JVM). The exact file loaded depends on the Java Virtual Machine you are using, and is selected by querying the name of the Java Virtual Machine the Java program is running under. Object COBOL provides support for the JVMs listed in Setting Up COBOL and Java in chapter Calling Java from COBOL.

If your JVM is not one of those listed as supported, you will get a fatal "Unsupported JVM" error. You can force the loading of a particular JVM by setting a Java system property, called mfcobol.cobjvm, to the name of one of the supported JVMs. For example, to load the support for the Sun JVM, contained in cbljvm_sun.dll, set this property to "sun".

You can set this property on the command line that runs your Java program, as follows:

java -Dmfcobol.cobjvm=name class

where name is the name of the support module you want to use. For example, to run Java program myclass with the support for the Sun JVM:

java -Dmfcobol.cobvjm=sun myclass

If you get the "unsupported JVM" error, try the Sun JVM first as many JVMs are rebadged from Sun.

7.3 Writing a Java Class in Object COBOL

To write a class in Object 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. The Object COBOL class must inherit from javabase.

  3. Create a Java wrapper class for the Object COBOL class. The wrapper class must have the same class name as the filename of the Object COBOL class.

  4. Each method you create in your Object COBOL class must have a corresponding method in the Java wrapper class.

  5. You must 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.

If you use the Net Express class wizard to create a Java class in Object COBOL, it creates the Object COBOL class with the correct inheritance and directives. It also creates the Java wrapper class. For more information about the class wizard, click Help Topics on the Net Express Help menu, then look up class wizard in the index.

7.3.1 Importing the COBOL Support

The support needed for the Java wrapper class to communicate with the Object COBOL class is in the mfcobol package. The wrapper must include the following statement:

import mfcobol.* ;

You must also ensure mfcobol.jar is on the Java classpath, or your Java programs will not compile or run (this is covered in Setting Up COBOL and Java in chapter Calling Java from COBOL).

7.3.2 The Wrapper Class

The Java wrapper class must extend either mfcobol.runtimeObject or mfcobol.runtime. This affects the lifetime of the Object 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 The name of the .dll file which contains the Object 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 The filename of the Object COBOL class.
FullJavaClassName The Java classname corresponding to the Object COBOL class.

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

7.3.3 Adding and Removing Methods

Every method you add to the COBOL class must have a corresponding function in the Java wrapper class. The Net Express method wizard adds methods to the Java wrapper class automatically if you use it to add methods to a COBOL Java class. However, if you subsequently delete a method from the COBOL class, you must delete it manually from the Java wrapper class.

For more information about the class wizard, click Help Topics on the Net Express Help menu, then look up class wizard in the index.

The rest of this section describes how you code a Java function in the wrapper class by hand, without the use of the method wizard.

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 Java Bean, 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 runtime.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 Object COBOL instances. Use the cobinvokestatic_ methods for invoking Object COBOL classes.

    See the Java Run-time Class Library for the full list of cobinvoke_ functions. Click Help Topics on the Net Express Help menu, and then click Reference, OO Class Library, Class Library Reference, and click the shortcut to start the Class Library Reference. .

    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.

Object COBOL class 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 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.

When you use the method wizard in Net Express to add methods to a COBOL class which has been generated as a Java class, you have the option of giving the method a different name in the Java wrapper and the COBOL class.

7.3.4 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 Class Library reference. Click Help Topics on the Net Express Help menu, and then click Reference, OO Class Library, Class Library Reference, and click the shortcut to start the Class Library Reference.

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 javaclassname
description

where :

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

7.4 COBOL Enterprise JavaBeans

The Net Express class wizard enables you to create a COBOL classes for use as an Enterprise JavaBean (EJB). EJBs are software components which run on application servers. The application server is responsible for all the services required by the bean, such as security, transaction integrity and persistence, so that EJBs only need to implement business logic. For more information about EJBs, see Sun's Java site.

When you check Enable for Enterprise JavaBeans on the Java Details page of the Net Express class wizard, it creates the following files:

When you generate an EJB, the wizard also adds five methods to your class:

These methods are part of the SessionBean interface, which is implemented by all EJBs. You need to add code to the first three of these methods; the wizard adds default code to SetSessionContext to store a Java context object as part of the instance data of your COBOL class.

EJBCreate is a method which can be overloaded in an EJB, enabling you to provide different parameters for initializing a Java bean on instantiation. The wizard adds an EJBCreate method, which takes no parameters, to your EJB and a corresponding create method to the remote interface class (classnameRemote.java). The create method on the classnameRemote.java returns a remote interface object.

Each method you add to the COBOL class must be added to the Java wrapper class, and also to the remote interface class. If you use the Net Express method wizard, it updates the wrapper and remote interface automatically for you.

7.5 Demonstration Programs

Net Express includes some demonstration programs that illustrate different aspects of calling Object COBOL from Java. These demonstration programs are contained in directories beneath \Program Files\MERANT\Net Express\Base\Demo\Javademo\Oocobol. 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 Object COBOL.
  • Creating a Java array in Object COBOL and passing it to Java.
simple
  • Writing a simple COBOL Java object.
  • Calling both class and instance Object COBOL methods using Java.
sort
  • Writing a simple COBOL Java object.
  • Calling Object COBOL methods using Java.
  • Calling Java methods using Object COBOL.
  • Dealing with Java objects as parameters and return values.


Copyright © 2000 MERANT International Limited. All rights reserved.
This document and the proprietary marks and names used herein are protected by international law.

PreviousCalling Java from COBOL Java Data TypesNext