You now need to create a new class file to contain the main method:
- Click the
Calc project, and then click
.
This opens the
New dialog box.
- Click
Class.
- Click
Next.
This opens the
New Java Class dialog box.
- In the
Package field, type
com.calc
- In the
Name field, type
MainClass.
- Click
Finish.
This opens the
MainClass.java in an editor.
- 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);
}
}
- 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.
- 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
.
- Click the
Libraries tab, and then click
Classpath.
- Click
Add Library.
This opens the
Add Library dialog box.
- Click
COBOL JVM Runtime System, and then click
Next.
This displays the path to the runtime to be used.
- Click
Finish.
- Click
Apply and Close.
- 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.
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:
- 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;