[an error occurred while processing this directive]

HP OpenVMS Systems

ask the wizard
Content starts here

Calling COBOL from Java? (JNI)

» close window

The Question is:

 
Can I call a Cobol program from a java code.
I yes, Where can I find related information, examples etc
 
 


The Answer is :

 
  Details on and pointers to Java on OpenVMS Alpha are included in the
  OpenVMS Frequently Asked Questions (FAQ) document.  Please start there.
 
  Here is an extension of the JNI example for use on OpenVMS Alpha.  (This
  example is an extension of the Compaq COBOL on Tru64 UNIX example.)
 
 
$! From JNI_EXAMPLE.COM
$ @SYS$COMMON:[JAVA$131.COM]JAVA$131_SETUP
$ SAY = "write sys$output "
$ say "1. compiling jni5.java"
$ javac "jni5.java"
$ say "2. generating header file jni5.h"
$ javah -jni jni5
$ say "3. compiling jni5c.c"
$ opts = "/prefix=all /float=ieee /ieee=denorm " + -
         "/names=as_is /reentrancy=multithread /stand=ms /nolist"
$ cc 'opts' /include=(SYS$COMMON:[JAVA$131.INCLUDE...]) -
  /obj=jni5c.obj jni5c.c
$ say "4. compiling jni5cob.cob"
$ opts = "/float=ieee /nolist"
$ cobol 'opts' jni5cob.cob
$ say "5. linking jni5"
$ GLB_OPT :== global_symbol_option
$ LIB_OPT :== library_option
$ @dcom$:scan_globals_for_option jni5*.obj 'GLB_OPT'.opt
$ OPEN/WRITE library_opt_file 'LIB_OPT'.opt
$ write library_opt_file "GSMATCH=LEQUAL,1,1"
$ write library_opt_file "jni5c.obj"
$ write library_opt_file "jni5cob.obj"
$ write library_opt_file "JAVA$JAVA_SHR/SHARE"
$ close library_opt_file
$ DEBUG_SWITCH :== /nodebug/notrace
$ IMAGE_NAME :== jni5
$ link/''DEBUG_SWITCH'/SHARE/exec=java$'IMAGE_NAME'_shr.exe -
  'LIB_OPT /OPT,'GLB_OPT /OPT /NOMAP
$ say "6. defining a logical for the new shareable image"
$ def/job/log java$'IMAGE_NAME'_shr sys$disk:[]java$'IMAGE_NAME'_shr.exe
$ say "7. running jni5"
$ java jni5
$done:
 
$ type jni5.cob
identification division.
program-id. JNI5COBLONG.
environment division.
data division.
linkage section.
01 llong pic s9(9) comp.
procedure division using llong.
p1.	add 1 to llong.
	exit program.
end program JNI5COBLONG.
 
identification division.
program-id. JNI5COBSTRING.
environment division.
data division.
working-storage section.
01 wloc pic 9(9) comp.
linkage section.
01 lstring pic x(100).
procedure division using lstring.
p2.	call "FINDNULL" using lstring giving wloc.
	move "2" to lstring(4:1).
	exit program.
identification division.
program-id. FINDNULL.
environment division.
data division.
working-storage section.
01 wloc pic 9(9) comp.
01 wsub pic x(100).
linkage section.
01 lstring pic x(100).
procedure division using lstring giving wloc.
p3.	unstring lstring delimited by "" into wsub count in wloc.
	exit program.
end program FINDNULL.
end program JNI5COBSTRING.
 
$ type jni5.java
class jni5 {
  public static native long   jni5long(long arg1);
  public static native String jni5string(String arg1);
 
  static {
    System.loadLibrary("jni5");
  }
 
  public static void main (String[] args) {
    System.out.println("Java JNI5 long   = " + Long.toString(jni5long(1)));
    System.out.println("Java JNI5 string = " + jni5string("Str1"));
  }
 
 
$ type jni5c.c
#include "jni5.h"
#include <stdio.h>
#include <string.h>
void JNI5COBLONG(long *arg1);
void JNI5COBSTRING(char *arg1);
 
JNIEXPORT jlong JNICALL Java_jni5_jni5long (JNIEnv *env, jclass this,
    jlong arg1) {
  long retlong;
  retlong = arg1;
  printf("C    JNI5 long   = %d (before call to COBOL)\n", retlong);
  JNI5COBLONG(&retlong);
  printf("C    JNI5 long   = %d (after  call to COBOL)\n", retlong);
  return retlong;
 
 
JNIEXPORT jstring JNICALL Java_jni5_jni5string (JNIEnv *env, jclass this,
    jstring arg1) {
  const char *tmp = (*env)->GetStringUTFChars(env, arg1, 0);
  char retstring[100];
  strcpy(retstring, tmp);
  printf("C    JNI5 string = %s (before call to COBOL)\n", retstring);
  JNI5COBSTRING(retstring);
  printf("C    JNI5 string = %s (after  call to COBOL)\n", retstring);
  (*env)->ReleaseStringUTFChars(env, arg1, tmp);
  return (*env)->NewStringUTF(env, retstring);
 
 

answer written or last revised on ( 25-MAR-2002 )

» close window