Examples of Calling COBOL from Java

Restriction: This applies to native code only.

This section shows you two short examples of calling a COBOL program from Java. The first example shows the following features:

This is a simple COBOL subroutine, named legacy.cbl:

working-storage section.
copy "javatypes.cpy". 
01 wsResult       jint.

linkage section.
01 wsOperand1     jint. *> type defined in javatypes.cpy 
01 wsOperand2     jint. 
01 wsOperation    pic x.

procedure division using wsOperand1 wsOperand2 wsOperation.
    evaluate wsOperation
    when "a"
        add wsOperand1 to wsOperand2 giving wsResult
    when "s"
        subtract wsOperand1 from wsOperand2 giving wsResult
    end-evaluate
    exit program returning wsResult.

This is a Java program which calls the subroutine:

import com.microfocus.cobol.* ;
class SimpleCall
{
   public static void main(String argv[]) throws Exception
   {
       int i = RuntimeSystem.cobcall_int("legacy", 
		            	new ParameterList()
	                   			.add((int)4
                      	.add((int)7
                   				.add((byte)'a'));
       System.out.println(i) ;
   }
}

The second example shows you how to pass data to a COBOL program with the equivalent of different usage clauses. The cobcall() method used does not return a value, but it takes an object as the first parameter, and returns the same object type from the COBOL program. SimpleCall2 passes the first parameter by reference, the second by value, and the third by content.

import com.microfocus.cobol.* ;

class SimpleCall2
{
   public static void main(String argv[]) throws Exception
   {
      RuntimeSystem.cobcall(null, "usages", 
                    new ParameterList()
                    .add((int)1, RuntimeSystem.BY_REFERENCE)
                    .add((int)2, RuntimeSystem.BY_VALUE)
                    .add((int)3, RuntimeSystem.BY_CONTENT));
   }
}