Java calling COBOL (separate projects)

The following example demonstrates a Java program from one project calling a COBOL program from another. It passes two arguments to the COBOL and accepts a return value.

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

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 JCall, 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 JCall 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-COBOL projects, and then click OK.

  2. Create the Java program (Demo3.java):
    1. Select the JCall project in the COBOL Explorer view and click File > New > Other > Class, and then click Next.
    2. Ensure that the Source folder field specifies JCall/src, in the Package field enter com.microfocus.java, in the Name field, enter Demo3, 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 com.mycompany.demo3.*;
      
      public class Demo3
      {
          public static void main(String[] args)
          {
            String[] s = {"hello", "there", "pink", "green"};
            int int1 = 23;
            System.out.println("---------demo3---------");
            int[] i = com.mycompany.demo3.progs.demo3(s, int1);
            System.out.println("Hello from Java");
            for (int k=0; k<i.length; k++)
                System.out.println(i[k]);
      
          }
      }

      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. At this point, there will be errors because the COBOL program does not yet exist. These will be resolved during the following steps.

  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 CCall, select a project template, and then click Finish.
      Note: The bitism of the selected project template must match that of the JRE selected in JCall.
    The CCall project is created and displayed in the workspace.
  4. Create the COBOL program (demo3.cbl):
    1. Select CCall in the COBOL Explorer view and click File > New > COBOL Program.
    2. In the New file name field, type demo3.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)
             >>JAVA-CALLABLE
             program-id. demo3 as "demo3" .
      
             working-storage section.
             01 i pic 9(9) comp-5.
             01 primes.
               03 pic 9(9) comp-5 occurs 10 value 2,3,5,7,11,13,17,19,23,27.
      
             linkage section.
             01 ltable1.
               03 str pic X(100) occurs 4.
             01 lint1 pic 9(9) comp-5.
             01 ltable2.
               03 int1 pic 9(9) comp-5 occurs 10.
             procedure division using ltable1 by value lint1 returning ltable2.
                 perform varying i from 1 by 1 until i > 4
                     display "COBOL " i " " str(i)
                 end-perform
                 display "COBOL " lint1
                 move primes to ltable2
             goback.
             end program demo3.
  5. Set the COBOL project properties:
    1. Ensure that CCall is selected, then on the Project menu, click Properties.

      The Properties for CCall dialog box appears.

    2. Select Micro Focus > Build Configurations > Link.

      The Link settings are displayed.

    3. Change the following settings, and then click Apply:
      Option Value
      Output name prefix the current value with 'lib'
      Target type Single Native Library File
      Callable by non-COBOL applications Yes
      Multi-threaded Yes
    4. Select Micro Focus > Project Settings > COBOL.
    5. Click Ellipsis button in the Additional directives field, and then add the following Compiler directives and click OK:
      • java-output-path"<path-to-src-folder-of-Java-project>"
      • java-package-name"com.mycompany.demo3"
    6. Click Apply and Close.

      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.

  6. In JCall, right-click the src folder and select Refresh.

    The demo3.native.sig file is displayed in a folder structure that represents the namespace; this is one of the files required by Java to call into the COBOL program.

  7. Configure the genjava utility:

    The genjava utility is required to produce some Java artifacts required that interoperate with the COBOL program.

    1. On the Run menu, point to External Tools, and then select External Tools Configurations.

      The Create, manage, and run configurations dialog box is displayed.

    2. Double-click Program.

      A new configuration is displayed.

    3. In the Name field, type genjava.
    4. In the Location field, click Browse File System and select the full path and executable name for the genjava utility.

      By default, the executable (genjava) is in the bin sub-folder of the product installation folder.

    5. In the Working Directory field, click Browse Workspace and select the src folder of the JCall project.
    6. In the Arguments field, enter the following arguments for the command:

      <COBOL-Output-Name> -p demo3 -k com.mycompany.demo3

    7. Click Run.

      Refresh the src folder in the JCall again and a progs.java file has been added.

  8. Create the run configuration, and run the application:
    1. Right-click the JCall and select Run As > Run Configurations.

      The Run Configurations dialog box is displayed.

    2. Double-click the Java 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. In the Main class field, enter com.microfocus.java.Demo3.
    5. Click the Arguments tab, and in the VM arguments field, enter:
      -Djava.library.path=<path-to-COBOL-project-output-folder> 

      replacing <path-to-COBOL-project-output-folder> with the full path name to the CCall project's output folder.

    6. Click Run.
    The following output is produced in the Console window:
    ---------demo3---------
    COBOL 0000000001 hello                                                                                               
    COBOL 0000000002 there                                                                                               
    COBOL 0000000003 pink                                                                                                
    COBOL 0000000004 green                                                                                               
    COBOL 0000000023
    Hello from Java
    2
    3
    5
    7
    11
    13
    17
    19
    23
    27

    The code and the output shows the Java program passing two arguments into the COBOL program, which are processed and displayed from the COBOL. The COBOL program also returns a value, which is then processed in the Java program.