Previous Topic Next topic Print topic


Example of JVM COBOL Calling Java

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 Java project for the 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.
  2. Add a class to the Java project:
    1. Click File > New > Other > Java > Class.
    2. Click Next.
    3. Specify com.microfocus.test in the Package field.
    4. Specify JavaCalculator as the class name.
    5. Click Finish.

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

  3. 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;
        }
    }
  4. In your Eclipse workspace, create a JVM COBOL project for the calling COBOL program:
    1. Click File > New > COBOL JVM Project.
    2. Type CobolProject as the project name.
    3. Click Finish.
  5. Add a new COBOL program to the JVM COBOL project:
    1. Click File > New > COBOL Program.
    2. Specify CobolCalculatorClient as the file name.
    3. Click Finish.

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

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

  7. To enable JVM COBOL to access Java, add the Java project to the build path of the JVM COBOL project as follows:
    1. Select your JVM COBOL project in the COBOL Explorer.
    2. Click Project > Properties.
    3. Click Micro Focus > JVM Build Path in the left-hand pane.
    4. Click the Projects tab.
    5. Click Add.
    6. Select your Java project from the list and click OK twice.
    7. Build the project. Note that there are no build errors this time.
  8. Run your COBOL program.
    1. Open the COBOL program, CobolCalculatorClient, in the editor.
    2. Click Run > Run As > COBOL JVM Application.
    3. The IDE displays the result in the Console window.
Previous Topic Next topic Print topic