Java calling COBOL (COBOL/Java Interoperability project)

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

To use the Java and COBOL from within separate projects, see Java calling COBOL (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. Set the package name in the project properties:
    Note: The package name is used to package up all the COBOL-generated Java artifacts required for the Java code to interoperate with the COBOL.
    1. Ensure that your project is selected, then on the Project menu, click Properties.

      The Properties for <project-name> dialog box appears.

    2. Select Micro Focus > Project Settings > COBOL.
    3. Update the Package Name field to com.mycompany.demo3.

      Other settings are required for this example, such as the Java Interoperability Output Path set to src, and the project being built to a single native library file, but these are the default settings and so are already set.

    4. Click Apply and Close.
  3. Create the COBOL program (demo3.cbl):
    1. Select the project 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.

      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 the Java program (Demo3.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.mycompany.demo3, 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.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.

  5. 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 Java Application launch configuration.

      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.mycompany.demo3.Demo3.
    5. Select the Arguments tab, and in the VM arguments field, enter the following argument:
      -Djava.library.path=<path-to-COBOL-output-folder> 

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

    6. Click Apply, and then 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.