Using the COBOL Program from Java

  1. You can use the ILCUTPREFIX directive to remove a specified prefix from the COBOL data items. In this case, you can use it to remove the Lnk prefix. This results in the class becoming Sum. To do this insert the following line at the top of the Calculator.cbl program:
    $set ILCUTPREFIX "lnk-"
  2. You can also use ILSMARTNEST, in conjunction with ILSMARTLINKAGE directive, to manage code as nested classes of the program class in which they are defined. In this case, it results in the LnkSum class becoming an internal class inside the Calculator class (my.pack.Calculator.LnkSum).

    The default option for input arguments is by reference. If both by value and by reference are omitted then the Compiler accepts the arguments as by reference type.

    To simplify the method signature you can group the arguments together. To do this, define a new group item, args:

           01 args

    Then, change the existing variables lnk-arg1, lnk-arg2, and lnk-sum to be 03 elementary items of the group:

           01 args
               03 lnk-arg1           pic 9(5) comp-3.
               03 lnk-arg2           pic 9(5) comp-3.
               03 lnk-sum            pic 9(5) comp-3.

    You can now replace all by value and by reference with the new group item - from:

           procedure division using by value lnk-arg1, 
                                    by value lnk-arg2,
                                    by reference lnk-sum.
    

    To:

           procedure division using args.

    Your Calculator.cbl program should now look like:

           $set ilsmartlinkage "my.pack"
           $set ilnamespace "my.pack"
           $set ilcutprefix "lnk-"
           program-id. Calculator as "Calculator".
    
           data division.
           working-storage section.
           
           linkage section.
           01 args
               03 lnk-arg1           pic 9(5) comp-3.
               03 lnk-arg2           pic 9(5) comp-3.
               03 lnk-sum            pic 9(5) comp-3.
    
           procedure division using args.
               add lnk-arg1 to lnk-arg2 giving lnk-sum.
               goback.
    
           end program Calculator.
  3. Also, update MainClass.java to import Args, and update the main method to use it:

    package com.calc;
    
    import my.pack.Calculator;
    import my.pack.Args;
    
    public class MainClass {
    
    	public static void main(String[] args) {
    		Calculator calc = new Calculator();
    		Args arguments = new Args();
    		arguments.setArg1(4);
    		arguments.setArg2(2);
    		calc.Calculator(arguments);
    		System.out.println(arguments.getSum());
    	}	
    }

You are ready to run the application.