COBOL calling Java static method (COBOL/Java Interoperability project)

The following example demonstrates a native COBOL program calling into a static Java method.

To use the Java and COBOL from within separate projects, see COBOL calling Java static method (separate projects).

  1. Create a COBOL/Java Interoperability project:
    1. Click File > New > Project, select COBOL/Java Interoperability Project type from the Micro Focus COBOL folder, and then click Next.
    2. In the Project name field, enter a name for the project, select a project template, and then click Next.
    3. Select a Java runtime to use with the project, and then click Finish.
    The project is created and displayed in the workspace. It is recommended to work in COBOL Explorer view with this type of project as it gives full visibility to all project artifacts by default.
  2. Create the COBOL program (demo.cbl):
    1. Select the project in the COBOL Explorer view and click File > New > COBOL Program.
    2. In the New file name field, type demo1.cbl, and then click Finish.

      The program is opened in the editor.

    3. Replace the text with the following, and then save the program.
            $set sourceformat(variable) nsymbol(national)
             program-id. demo1 as "demo1".
             78 MAX_OCC value 5.
             01 i pic xxxx comp-5.
             01 grp2.
                03 nat1 pic n(10) national occurs MAX_OCC
                       value n"Red" n"Green" n"Blue" n"Orange" n"Indigo".
             01 grp3.
               03 utf1 pic u(10) occurs MAX_OCC.
             01 grp4.
               03 num2 pic xxxx comp-5 occurs MAX_OCC
                        value 1 2 4 5 6. 
             procedure division.
            *> Sort COBOL array
              call "java.com.microfocus.java.Demo1.static1" using grp2
              display "---Output from COBOL---"
              perform varying i from 1 by 1 until i > 5
                  display nat1(i)
              end-perform
            *> Select colors of the rainbow from input array
            *> Careful Java has 0 based array indexes
               call "java.com.microfocus.java.Demo1.static2" using grp4 returning grp3
               display "---Output from COBOL---"
               perform varying i from 1 by 1 until i > 5
                  display utf1(i)
               end-perform.
             end program demo1.        

      If your workspace is set to build automatically, the program is compiled. If the workspace is not set to build automatically, on the Project menu, click Build Project.

  3. Create the Java program (Demo1.java):
    1. Select the project in the COBOL Explorer view and click File > New > Other > Class, and then click Next.
    2. Ensure that the Source folder field specifies <project-name>/src, in the Package field enter com.microfocus.java, in the Name field, enter Demo1, then click Finish.

      The program is opened in the editor.

    3. Replace the text with the following, and then save the program.
      package com.microfocus.java;
      
      import java.util.Arrays;
      
      public class Demo1
         {
          public static void static1(String[] d)
          {
              System.out.println("---Output from Java Demo1.static1 method---");
              for (int i = 0; i < d.length; i++)
              {
                  System.out.println(d[i]);
              }
              Arrays.sort(d);
          }
          /* select colours from array */
          public static String[] static2(int[] s)
          {
              System.out.println("---Output from Java Demo1.static2 method---");
              for (int i: s)
                  System.out.println(i);
              String[] rainbow = {"Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet"};
              String[] ret = {rainbow[s[0]], rainbow[s[1]], rainbow[s[2]], rainbow[s[3]], rainbow[s[4]]};
              return ret;
          }
      }

      If your workspace is set to build automatically, the program is compiled. If the workspace is not set to build automatically, on the Project menu, click Build Project.

  4. Create and execute the run configuration:
    1. Right-click the project in the COBOL Explorer view and select Run As > Run Configurations.
    2. Double-click the COBOL/Java Interoperability Application launch configuration type in the left-hand pane.

      A new configuration is displayed in the right-hand pane.

    3. In the Name field, enter a name for the configuration.
    4. Select the Environment tab, and then add the following variable:
      Variable Value
      COBSW +S5
    5. Click Apply, and then click Run.
    The following output is produced in the Console window:
    ---Output from Java Demo1.static1 method---
    Red
    Green
    Blue
    Orange
    Indigo
    ---Output from COBOL---
    Blue
    Green
    Indigo
    Orange
    Red
    ---Output from Java Demo1.static2 method---
    1
    2
    4
    5
    6
    ---Output from COBOL---
    Orange
    Yellow
    Blue
    Indigo
    Violet

    The code and the output shows the COBOL code executing the static1 method, processing some COBOL operations, then executing the static2 method, and processing some more COBOL operations.