The following example shows how to create a Java project and enable it to access a JVM COBOL program compiled to JVM byte code.
- In your Eclipse workspace, create a Java project for the Java program:
- Click
File > New > Project > Java > Java Project.
- Click
Next.
- Specify
JavaProject as the project name
- Click
Finish.
- Click
No in the Open Associated Perspective dialog box.
- Add a class to the Java project:
- Click
File > New > Other > Java > Class.
- Click
Next.
- Specify
com.microfocus.test in the
Package field.
- Specify
JavaCalculator as the class name.
- Click
Finish.
This adds the class to your Java project and opens it in the editor.
- Paste the following code in the Java class:
package com.microfocus.test;
public class JavaCalculator {
public int add(int first, int second) {
return first + second;
}
}
- In your Eclipse workspace, create a JVM COBOL project for the calling COBOL program:
- Click
File > New > COBOL JVM Project.
- Type
CobolProject as the project name.
- Click
Finish.
- Add a new COBOL program to the JVM COBOL project:
- Click
File > New > COBOL Program.
- Specify
CobolCalculatorClient as the file name.
- Click
Finish.
This adds the COBOL program to your project and opens it in the editor.
- Add the following code snippet to the newly generated class:
program-id. CobolCalculatorClient as "CobolCalculatorClient".
data division.
working-storage section.
01 calculator type com.microfocus.test.JavaCalculator.
01 result pic 99.
procedure division.
set calculator to new com.microfocus.test.JavaCalculator()
set result to calculator::add(10, 5)
display result
goback.
end program CobolCalculatorClient.
Note that the COBOL code defines a variable of the type JavaCalculator which is defined in the com.microfocus.test package. However, because the interoperation between the Java and the JVM COBOL projects has not been enabled yet, the Java class is not recognized and the code above generates errors.
- To enable JVM COBOL to access Java, add the Java project to the build path of the JVM COBOL project as follows:
- Select your JVM COBOL project in the COBOL Explorer.
- Click
Project > Properties.
- Click
in the left-hand pane.
- Click the
Projects tab.
- Click
Add.
- Select your Java project from the list and click
OK twice.
- Build the project. Note that there are no build errors this time.
- Run your COBOL program.
- Open the COBOL program,
CobolCalculatorClient, in the editor.
- Click
Run > Run As > COBOL JVM Application.
- The IDE displays the result in the Console window.