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 JVM COBOL project for the COBOL program:
- Click
File > New > COBOL JVM Project.
- Type
CobolProject as the project name.
- Click
Finish.
- Add a COBOL class to the JVM COBOL project:
- Click
File > New > COBOL JVM Class.
- Specify
com.microfocus.test in the
Package field.
- Specify
CobolCalculator as the class name.
- Click
Finish.
This adds the class to your project and opens it in the editor.
- Add the following code snippet to the newly generated class:
class-id com.microfocus.test.CobolCalculator public.
working-storage section.
method-id add.
local-storage section.
procedure division using by value firstNumber as binary-long,
by value secondNumber as binary-long,
returning result as binary-long.
add firstNumber to secondNumber giving result
goback.
end method.
end class.
- In your Eclipse workspace, create a Java project for the calling 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.javademo in the
Package field.
- Specify
JavaCalculatorClient 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.javademo;
public class JavaCalculatorClient {
/**
* @param args
*/
public static void main(String[] args) {
CobolCalculator cobolCalculator = new CobolCalculator();
System.out.println(cobolCalculator.add(10, 5));
}
}
Note that the Java code here uses the CobolCalculator class defined in the JVM COBOL project. However, because the interoperation
between the Java and the JVM COBOL projects has not been enabled yet, the COBOL class is not recognized and the code above
generates errors.
- To enable Java to access JVM COBOL, add the JVM COBOL project to the Java build path of the Java project as follows:
- Select your Java project and click
Project > Properties.
- Click
Java Build Path in the left-hand pane.
- Click the
Projects tab.
- Click
Add.
- Select your COBOL JVM project from the list and click
OK twice.
- Add
mfcobol.jar and the JVM COBOL Runtime,
mfcobolrts.jar, to the project's classpath:
- Select the Java project and click
Project > Properties.
- Select
Java Build Path.
- Click the
Libraries tab.
- Click
Add External JARs and select
mfcobol.jar and
mfcobolrts.jar in
<installdir>\bin (Windows) or
$COBDIR/lib (Linux).
- Click
Open and then
OK.
- Add an import statement in the Java source file to import the COBOL class:
- Open the Java class,
JavaCalculatorClient.java, in the editor.
- Insert the following code at the top of the file:
import com.microfocus.test.CobolCalculator;
Tip: This is the same as using the Eclipse Quick Fix feature in the editor:
- Right-click the unresolved class,
CobolCalculator, in the editor.
- Select
Quick Fix. This opens Content Assist which offers you several options to resolve the problem.
- Double-click
Import 'CobolCalculator' (com.microfocus.test) in the list. This adds an import statement in the code for the COBOL class.
- Build the project. Note that there are no build errors this time.
- Run your Java program.
- Click
Run > Run As > Java Application.
- The IDE displays the result in the Console window.