Example of Java Calling JVM COBOL

The following example shows how to create a Java project and enable it to access a JVM COBOL program compiled to JVM byte code.

  1. In your Eclipse workspace, create a JVM COBOL project for the COBOL program:
    1. Click File > New > COBOL JVM Project.
    2. Type CobolProject as the project name.
    3. Click Finish.
  2. Add a COBOL class to the JVM COBOL project:
    1. Click File > New > COBOL JVM Class.
    2. Specify com.microfocus.test in the Package field.
    3. Specify CobolCalculator as the class name.
    4. Click Finish.

      This adds the class to your project and opens it in the editor.

  3. Replace the code in the class with the following and save the file:
           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.
  4. In your Eclipse workspace, create a Java project for the calling Java program:
    1. Click File > New > Project > Java > Java Project.
    2. Click Next.
    3. Specify JavaProject as the project name
    4. Click Finish.
    5. Click No in the Open Associated Perspective dialog box.
  5. Add a class to the Java project:
    1. Click File > New > Other > Java > Class.
    2. Click Next.
    3. Specify com.microfocus.javademo in the Package field.
    4. Specify JavaCalculatorClient as the class name.
    5. Click Finish.

      This adds the class to your Java project and opens it in the editor.

  6. 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.

  7. To enable Java to access JVM COBOL, add the JVM COBOL project to the Java build path of the Java project as follows:
    1. Select your Java project and click Project > Properties.
    2. Click Java Build Path in the left-hand pane.
    3. Click the Projects tab.
    4. Click Add.
    5. Select your COBOL JVM project from the list, and click Apply and Close.
  8. Add mfcobol.jar and the JVM COBOL Runtime, mfcobolrts.jar, to the project's classpath:
    1. Select the Java project and click Project > Properties.
    2. Select Java Build Path.
    3. Click the Libraries tab.
    4. Click Add External JARs and select mfcobol.jar and mfcobolrts.jar in %ProgramFiles(x86)%\Micro Focus\Enterprise Developer\bin.
    5. Click Open, and then click Apply and Close.
  9. Add an import statement in the Java source file to import the COBOL class:
    1. Open the Java class, JavaCalculatorClient.java, in the editor.
    2. Insert the following code on the line after the Package statement:
      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.
    3. Build the project. Note that there are no build errors this time.
  10. Run your Java program.
    1. Click Run > Run As > Java Application.
    2. The IDE displays the result in the Console window.