Java calling COBOL and accessing COBOL working-storage (separate projects)

The following example demonstrates a Java program from one project calling two COBOL programs in another project, and also accessing and updating their shared working-storage items.

To use the Java and COBOL from within the same project, see Java calling COBOL and accessing COBOL working-storage (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, type JCallShare, 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 JCallShare 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 (Demo4.java):
    1. Select JCallShare in the COBOL Explorer view and click File > New > Other > Class, and then click Next.
    2. Ensure that the Source folder field specifies JCallShare/src, in the Package field enter com.microfocus.java, in the Name field, enter Demo4, 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.demo4.*;
      import java.math.BigDecimal;
      
      public class Demo4
      {
          public static void main(String[] args)
          {
            System.out.println("--Java--");
            int i = strg.demo4.Int1.get();
            System.out.println("Int1= " + i);
            strg.demo4.Int1.put(10201);
      
            String s = strg.demo4.Alpha1.get();
            System.out.println("Alpha1= " + s);
            strg.demo4.Alpha1.put("Purple");
      
            strg.demo4.Group1.colour1[2].put("Yellow");
            strg.demo4.Group1.colour1[3].put("Cyan");
      
            BigDecimal num1 = strg.demo4.Num1.get();
            System.out.println("Num1= " + num1);
            strg.demo4.Num1.put(new BigDecimal("1579.1113"));
      
            String n = strg.demo4.Nat1.get();
            System.out.println("Nat1= " + n);
            strg.demo4.Nat1.put("abcdeFGHIJ");
      
            String i1 = strg.demo5.Group1.Group2.item1.get();
            System.out.println("Item1= " + i1);
            strg.demo5.Group1.Group2.item1.put("Orange");
      
            String i2 = strg.demo5.Group1.Group2.Group3.item2.get();
            System.out.println("Item1= " + i2);
            strg.demo5.Group1.Group2.Group3.item2.put("Yellow");
      
            progs.demo4();
            progs.demo5();
          }
      }

      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, type CCallShare, and then click Finish.
      Note: The bitism of the selected project template must match that of the JRE selected in the JCallShare project.
    The CCallShare project is created and displayed in the workspace.
  4. Create the first COBOL program (demo4.cbl):
    1. Select the CCallShare project in the COBOL Explorer view and click File > New > COBOL Program.
    2. In the New file name field, type demo4.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 nsymbol(national) 
             >>JAVA-CALLABLE
             program-id. demo4 as "demo4".
             working-storage section.
             >>JAVA-SHAREABLE ON
             01 Int1 pic 9(9) comp-5 value 10.
             01 Alpha1 pic x(100) value "Pink".
             01 Group1.
                03 colour1 pic x(10) occurs 10 value "Red" "Green" "Blue".
             01 Num1 pic S9(5)v9(5) comp-3 value 3141.59265.
             01 Nat1 pic n(20) value n"Unicode" national.
             >>JAVA-SHAREABLE OFF
             01 ed-num1 pic +9(5).9(5).
             procedure division.
                 display "--In COBOL Program demo4--"
                 display "Int1= " Int1
                 display "Alpha1= " Alpha1
                 display "colour1(1)= "colour1(1)
                 display "colour1(2)= "colour1(2)
                 display "colour1(3)= "colour1(3)
                 move num1 to ed-num1
                 display "Num1= " ed-num1
                 display "Nat1= " Nat1
             goback.
             end program demo4.
  5. Create the second COBOL program (demo5.cbl):
    1. Select the CCallShare project in the COBOL Explorer view and click File > New > COBOL Program.
    2. In the New file name field, type demo5.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 nsymbol(national) 
             >>JAVA-CALLABLE
             program-id. demo5 as "demo5".
             working-storage section.
             >>JAVA-SHAREABLE ON
             01 Group1.
                03 Group2.
                  05 item1 pic x(10) value "Purple".
                  05 Group3.
                    07 item2 pic x(10) value "Green".
             >>JAVA-SHAREABLE OFF
             01 3item pic 9(10).
             procedure division.
                 display "--In COBOL Program demo5--"
                 display "Item1= " item1
                 display "Item2= " item2
             goback.
             end program demo5.
  6. Set the COBOL project properties:
    1. Ensure that CCallShare is selected, then on the Project menu, click Properties.

      The Properties for CCallShare dialog box appears.

    2. Select Micro Focus > Build Configurations > Link.

      The Link settings are displayed.

    3. Change the following settings, and then click Apply:

      Windows:

      Option Value
      Target type Single Native Library File

      UNIX:

      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.demo4"
    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. The two projects should now build without any errors.

  7. In the JCallShare project, right-click the src folder and select Refresh. The demo4.native.sig, demo5.native.sig, demo4ws.java, and demo5ws.java files are displayed in a folder structure that represents the namespace; these are files required by Java to call into the COBOL programs and work with the COBOL data items.
  8. Configure the genjava utility: The genjava utility is required to produce some Java artifacts 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 JCallShare.
    6. In the Arguments field, enter the following arguments for the command:

      <COBOL-Output-Name> -p demo4 demo5 -s demo4 demo5 -k com.mycompany.demo4

    7. Click Run.

      Refresh the src folder in JCallShare again and progs.java and strg.java files have been added.

  9. Create the run configuration, and run the application:
    1. Right-click JCallShare 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.Demo4.
    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 CCallShare project's output folder.

    6. Click Run.
    The following output is produced in the Console window:
    --Java--
    Int1= 10
    Alpha1= Pink                                                                                                
    Num1= 3141.59265
    Nat1= Unicode             
    Item1= Purple    
    Item1= Green     
    --In COBOL Program demo4--
    Int1= 0000010201
    Alpha1= Purple                                                                                              
    colour1(1)= Red       
    colour1(2)= Green     
    colour1(3)= Yellow    
    Num1= +01579.11130
    Nat1= abcdeFGHIJ          
    --In COBOL Program demo5--
    Item1= Orange    
    Item2= Yellow

    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.