Highlighted
Respected Contributor.
Respected Contributor.
2106 views

Calling a java library from legacy cobol with VC2.1

Jump to solution

Hello,

We would like to integrate the iText pdf java library in a legacy application developed under VC2.1.

We know - thanks to the forum - how to call iText in a cobol jvm project.

But it is me unclear how to call - if possible - a java library from legacy cobol (or oo cobol) gnt executables without developing the entire application for the jvm.

The idea would be :

program-id. myLegacyCode.

*> declare the java iText library

*> or an object referring to the library

*> or something calling a cobol program written using the cobol jvm syntax

procedure division.

*> open my data file

perform until eof

     read mydatafile-record next

     not at end

          call or invoke the java iText routines directly or via some glue

     at end

         set eof to true

      end-read

end-perform

*> close mydatafile

*> exit program

end program myLegacyCode.

The code above would be compiled to gnt but call a java library.

If it is possible, a skeleton program would help me understand how to proceed.

Thank you.

AR

0 Likes
1 Solution

Accepted Solutions
Highlighted
Respected Contributor.
Respected Contributor.

RE: Calling a java library from legacy cobol with VC2.1

Jump to solution

Thank you Chris.

So, if I understand correctly, if I want to integrate our legacy cobol with java, I can

- either follow the "legacy" example given in your demo program with the jvm,

- or use the old oo programming way in native application

Kind regards.

View solution in original post

0 Likes
4 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Calling a java library from legacy cobol with VC2.1

Jump to solution

You can call Java classes directly from a native application but it is not as simple as calling them from a COBOL JVM class as you must use the native Java domain support and the older style of OO programming using a repository, no type keyword, etc.

This support is covered in the documentation under Programming-->Compatibility-->Java and COBOL-->Calling Java from OO COBOL

Thanks.

0 Likes
Highlighted
Respected Contributor.
Respected Contributor.

RE: Calling a java library from legacy cobol with VC2.1

Jump to solution

Thank you Chris.

So, if I understand correctly, if I want to integrate our legacy cobol with java, I can

- either follow the "legacy" example given in your demo program with the jvm,

- or use the old oo programming way in native application

Kind regards.

View solution in original post

0 Likes
Highlighted
Respected Contributor.
Respected Contributor.

RE: Calling a java library from legacy cobol with VC2.1

Jump to solution

Chris,

It should be possible to call a java class from native gnt cobol using the JNI interface.

But how to proceed using JNI_CreateJavaVM ?

Is there a way to do in cobol something like the following in C :

#include <stdio.h>

#include <jni.h>

JNIEnv* create_vm() {

  JavaVM* jvm;

  JNIEnv* env;

  JavaVMInitArgs args;

  JavaVMOption options[1];

  /* There is a new JNI_VERSION_1_4, but it doesn't add anything for the purposes of our example. */

  args.version = JNI_VERSION_1_2;

  args.nOptions = 1;

  options[0].optionString = "-Djava.class.path=c:\\projects\\local\\inonit\\classes";

  args.options = options;

  args.ignoreUnrecognized = JNI_FALSE;

  JNI_CreateJavaVM(&jvm, (void **)&env, &args);

  return env;

}

void invoke_class(JNIEnv* env) {

  jclass helloWorldClass;

  jmethodID mainMethod;

  jobjectArray applicationArgs;

  jstring applicationArg0;

  helloWorldClass = (*env)->FindClass(env, "example/jni/InvocationHelloWorld");

  mainMethod = (*env)->GetStaticMethodID(env, helloWorldClass, "main", "([Ljava/lang/String;)V");

  applicationArgs = (*env)->NewObjectArray(env, 1, (*env)->FindClass(env, "java/lang/String"), NULL);

  applicationArg0 = (*env)->NewStringUTF(env, "From-C-program");

  (*env)->SetObjectArrayElement(env, applicationArgs, 0, applicationArg0);

  (*env)->CallStaticVoidMethod(env, helloWorldClass, mainMethod, applicationArgs);

}

int main(int argc, char **argv) {

  JNIEnv* env = create_vm();

  invoke_class( env );

}

We would like to invoke one java class from a native cobol program and I think it can be a simple solution to write and test the java on one side, and then call it from our cobol program.

Kind regards.

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Calling a java library from legacy cobol with VC2.1

Jump to solution


You can either compile the COBOL as bytecode with then jvmgen directive or if you want to use native code, then the simplest way is to the object COBOL java support, below is simple example that looks at the java os.name property.

      $set ooctrl"+p-f"
       IDENTIFICATION DIVISION.
       PROGRAM-ID.  TEST-FD.
       ENVIRONMENT DIVISION. CONFIGURATION SECTION.
       class-control.
      $if nojvmgen set
          Sys is class "$java$java.lang.System"
      $else
          Sys is class "java.lang.System"
      $end
           .
       DATA DIVISION.
       FILE SECTION.
       WORKING-STORAGE SECTION.
       77  pic-x-item  pic x(50).
       thread-local-storage section.
       01 anInt  pic x(5) comp-5.
       PROCEDURE DIVISION.
           invoke Sys "getProperty" using "os.name" giving pic-x-item.
           display "Operating System: " pic-x-item.
           000-EXIT-TPR.
           STOP RUN.



0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.