Creating a New Class to Contain the Main Method

You now need to create a new class file to contain the main method:

  1. Click the Calc project, and then click File > New > Other.

    This opens the New dialog box.

  2. Click Class.
  3. Click Next.

    This opens the New Java Class dialog box.

  4. In the Package field, type com.calc
  5. In the Name field, type MainClass.
  6. Click Finish.

    This opens the MainClass.java in an editor.

  7. You can now import the my.pack.Calculator class and access it from this Java project. Copy and paste the following code into the editor, overwriting the existing text:
    package com.calc;
    
    import my.pack.Calculator;
    
    public class MainClass {
    
        public static void main(String[] args) {
        	Calculator calc = new Calculator();
        	int result = 0;
        	calc.Calculator(1,2,result);
        	System.out.println(result);
        }
    
    	
    }
  8. The Calculator class has a method that correspond to the procedure divisions in the Calculator.cbl program:
    long my.pack.Calculator.Calculator(int lnk-arg1, int lnk-arg2, int lnk-sum)
    The result variable will always be equal to 0 because it uses the by value option for the lnk-sum argument in the procedure division of the Calculator.cbl program. To get a return value, set the variable used for result to be passed as by reference. Modify the procedure division in the Calculator.cbl file to:
           procedure division using by value lnk-arg1, 
                                    by value lnk-arg2,
                                    by reference lnk-sum.
  9. At this point you get a problem marker with a description saying The type com.microfocus.cobol.program.Reference cannot be resolved.. To resolve this, you need to add the COBOL JVM Runtime System library. Right-click the Calc project, and then click Properties > Java Build Path.
  10. Click the Libraries tab.
  11. Click Add Library.

    This opens the Add Library dialog box.

  12. Click COBOL JVM Runtime System, and then click Next.

    This displays the path to the runtime to be used.

  13. Click Finish.
  14. Click Apply and Close.
  15. Instead of a reference type, you can specify the ILSMARTLINKAGE directive. This exposes the linkage section and entry points to the managed code by creating types. In the Calculator.cbl file, on the first line at column 7, type:
    $set ILSMARTLINKAGE "my.pack"
    Note: The each $set must be on a newline.
    • Alternatively, you can add the directive to the project's build configuration:
      1. Right-click the CJVM project, and then click Properties > Micro Focus > Build Configuration.
      2. In the Additional directives field, type ILSMARTLINKAGE “my.pack”.
      3. Click Apply and Close.
      Note: Using this method applies the directive to all programs in the project.
    Using this directive results in a new class being created, called LnkSum, which is created in the my.pack package. You can see this in the COBOL Explorer by expanding the Calculator.cbl file:
    LnkSum
  16. The calculator's method now requires the following signature:
    (int lnk-arg1, int lnk-arg2, LnkSum my.pack.LnkSum)
    Modify the main method in the MainClass.java file:
    	public static void main(String[] args) {
    		Calculator calc = new Calculator();
    		LnkSum sum = new LnkSum();
    		calc.Calculator(1,2,sum);
    		System.out.println(sum.getLnkSum());
    	}
    Also, ensure that you add the following import:
    import my.pack.LnkSum;