Example 2: Locating the Java Property File Directly from COBOL

This example demonstrates how the JVM COBOL run-time system uses the Java property file, created using the mfjarprogmap utility, to call a program that was compiled as part of a package. It shows how you can call a .jar file containing a Java property file, directly from COBOL.

Note: The following example is designed to be run from a Visual COBOL command prompt.
  1. Copy and paste the following code into a new file, and save it as myapp.cbl.
            program-id myapp.
            procedure division.
            display "Hello world from myapp".
            goback.
            end program myapp.
  2. Create a trigger program to call the previous program: copy and paste the following code into a new file, and save it as caller.cbl.
            program-id caller.
            01 p procedure-pointer.
            01 runner pic x(10).
            procedure division.
            set p to entry "myapp.jar".
            move "myapp" to runner.
            call runner.
            goback.
            end program caller.
  3. Create a sub-directory in which to compile the source, and in which the Java property file will be created.
    mkdir bin
  4. Compile the source and trigger programs.
    cob -j caller.cbl
    cob myapp.cbl -C 'jvmgen(sub)' -C 'iloutput(bin)' 
                  -C 'ilnamespace(com.mycompany.desktopapp)'
    A caller.class file is created in your current directory, and the myapp.class file is created in the bin/com/mycompany/desktopapp directory.
  5. Create a .jar file that contains the contents of the bin directory.
    jar cvf myapp.jar -C bin/ .
    myapp.jar is created in the current directory.
  6. Create the Java property file, adding it to the .jar file in the process.
    mfjarprogmap -verbose -jar myapp.jar

    The mfcobolprogmap.properties file is created and added to myapp.jar.

  7. Run the trigger application.
    java caller
    The caller.class file runs, then calls myapp.jar, and because that .jar file contains the Java property file, the run-time system uses it to locate the called program in its packaged location. The result is that the Hello world from myapp message is displayed.