COBOL calling Java static method (separate projects)

The following example demonstrates a native COBOL program from one project calling into a static Java method from another project.

To use the Java and COBOL from within the same project, see COBOL calling Java static method (COBOL/Java Interoperability project).

Enhancements have been made to the CALL statement to allow you to call Java static methods that are within the same workspace.
For demonstration purposes, this example creates two new projects, but this example can also be adapted to use existing native COBOL and Java projects.
  1. Create the Java project:
    1. Click File > New > Other, and then select Java Project from the Java folder.

      The Create a Java Project wizard is displayed.

    2. In the Project name field, enter JStatic, select the required JRE for the project, and then click Next.
      Note: The bitism of the selected JRE must match that of the COBOL project you intend to create.
    3. On the Libraries tab, select Classpath, and then click Add Library.
    4. Double-click COBOL JVM Runtime System, and then click Finish.
    5. Click Finish.

    If you are prompted to open the Java perspective, select No. The JStatic project is created. To show both COBOL and Java projects, select the COBOL Explorer view, click View menu icon (View menu), and then click Filters and Customization. This opens the Filters and Customization dialog box. Click the Pre-set filters tab and uncheck Non-Micro Focus projects, and then click OK.

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

  3. Create the native COBOL project:
    1. Click File > New > COBOL Project.

      The COBOL Project wizard is displayed.

    2. In the Project name field, enter CStatic, select a project template, and then click Finish.
    The CStatic 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.
  4. Create the COBOL program (demo1.cbl):
    1. Select CStatic 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.

  5. Create and execute the run configuration:
    1. Right-click CStatic in the COBOL Explorer view and select Run As > Run Configurations.
    2. Double-click the COBOL 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 variables:
      Variable Value
      JAVA_HOME <full-path-to-JRE-used-in-Java-proj>
      CLASSPATH %CLASSPATH%;<path-to-Java-proj-output-folder>
    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.