Java accessing COBOL working-storage (separate projects)

The following example demonstrates a Java program from one project accessing a COBOL program's working-storage items from another project, and displaying them within Java and updating their values.

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 JShare, select the required JRE for the project.
      Note: The bitism of the selected JRE must match that of the COBOL project you intend to create.
    3. In the Module section, ensure that the Create module-info.java file check box is not selected, and then click Next.
    4. On the Libraries tab, select Classpath, and then click Add Library.
    5. Double-click COBOL JVM Runtime System, and then click Finish.
    6. Click Finish.

    If you are prompted to open the Java perspective, select No. The JShare 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 (Demo2.java):
    1. Select the JShare project in the COBOL Explorer view and click File > New > Other > Class, and then click Next.
    2. Ensure that the Source folder field specifies JShare/src, in the Package field enter com.microfocus.java, in the Name field enter Demo2, 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.demo2.strg;
      
      public class Demo2
       {
         public static void main(String[] args)
          {
            System.out.println("--COBOL items now accessible in Java--"); 
            int i1 = strg.demo2.grp1.i1.get();
            int i2 = strg.demo2.grp1.i2.get();      
            System.out.println("-- COBOL item i1 = " + i1);
            System.out.println("-- COBOL item i2 = " + i2);
            strg.demo2.grp1.i1.put(77777777);
            System.out.println("-- i1 updated from Java = " + strg.demo2.grp1.i1.get());
          }
       }

      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 CShare, select a project template, and then click Finish.
      Note: The bitism of the selected project template must match that of the JRE selected in the JShare project.
    The CShare project is created and displayed in the workspace.
  4. Create the COBOL program (demo2.cbl):
    1. Select CShare in the COBOL Explorer view and click File > New > COBOL Program.
    2. In the New file name field, type demo2.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)
             program-id. demo2 as "demo2".
             working-storage section.
             >>JAVA-SHAREABLE ON 
             01 grp1.
                03 i1 pic 9(8) comp-5 value 88888888.
                03 i2 pic 9(8) comp-5 value 12345678.
             >>JAVA-SHAREABLE OFF
             01 grp2.
                03 p1 pic x.
                03 p2 pic 9. 
             procedure division.
             display "start".
             display "value of shared CBL grp is: "
             display grp1::i1.
             display grp1::i2.
             end program demo2.
  5. Set the COBOL project properties:
    1. Ensure that the CShare project is selected, then on the Project menu, click Properties.

      The Properties for CShare 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.demo2"
    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 the JShare project, right-click the src folder and select Refresh.

    The demo2ws.java file is displayed in a folder structure that represents the namespace.

  7. 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 the JShare project.
    6. In the Arguments field, enter the following arguments for the command:

      <COBOL-Output-Name> -s demo2 -k com.mycompany.demo2

    7. Click Run.

      Refresh the src folder in the JShare project again and a strg.java file has been added. At this point, the errors in the Java program are now resolved.

  8. Create the run configuration, and run the application:
    1. Right-click JShare 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.Demo2.
    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 CShare project's output folder.

    6. Click Run.
    The following output is produced in the Console window:
    --COBOL items now accessible in Java--
    -- COBOL item i1 = 88888888
    -- COBOL item i2 = 12345678
    -- i1 updated from Java = 77777777

    The code and the output shows that the COBOL program has shared two PIC 9 COMP-5 items with the Java program. The Java program has used the get method to view the value of the COBOL data (to do this, the COMP-5 item was mapped to int types, as per Mapping COBOL Items and Java Types). The Java program then used the put method to change the value of the COBOL item (where again the mapping process was used, under the covers).