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 JCall 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-Micro Focus projects, and then click OK.
The program is opened in the editor.
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.
The COBOL Project wizard is displayed.
The program is opened in the editor.
$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.
The Properties for CCall 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 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.
The genjava utility is required to produce some Java artifacts required 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> -p demo3 -k com.mycompany.demo3
Refresh the src folder in the JCall again and a progs.java file has been added.
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 CCall project's output folder.
---------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.