To use the Java and COBOL from within the same project, see Java calling COBOL (COBOL/Java Interoperability project).
The Create a Java Project wizard is displayed.
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), 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.
The program is opened in the editor.
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.
The COBOL Project wizard is displayed.
The program is opened in the editor.
$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.
The Properties for CShare dialog box appears.
The Link settings are displayed.
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 |
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 demo2ws.java file is displayed in a folder structure that represents the namespace.
The genjava utility is required to produce some Java artifacts that interoperate with the COBOL program.
The Create, manage, and run configurations dialog box is displayed.
A new configuration is displayed.
By default, the executable (genjava) is in the bin sub-folder of the product installation folder.
<COBOL-Output-Name> -s demo2 -k com.mycompany.demo2
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.
The Run Configurations dialog box is displayed.
A new configuration is displayed in the right-hand pane.
-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.
--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).