|
|
COBOL Spices Up JavaBy Wilbert T. Kho, IBM
So where does COBOL fit in all of this? Information technology was not invented by Java and the WWW, although it may have been catapulted into the consciousness of many by these new technologies. A lot of corporations depend on so called legacy, mission-critical applications that were, and still are written in COBOL. These businesses run their operations on data created by these COBOL applications. The wholesale conversion of these applications and data to Java is a very daunting task and is not a very judicious use of company resources. Thus, a way of interfacing Java with COBOL is needed. What is the path to COBOL enlightenment?The creators of Java recognized the need to interface to non-Java methods and provided for this in the form of native methods. Native methods can be implemented in any non-Java language that supports the C calling conventions. The native methods are packaged in a function library such as dynamic link libraries (DLLs) on OS/2 and Windows 95 and shared libraries on AIX. We will use native methods as our entry point to the business domain knowledge and information that is encapsulated in the COBOL applications and data. In the next few sections, we will examine in detail the steps needed to interface Java with COBOL. I will summarize the high level tasks here.
If you are interested in reading more about native methods, please see Tricks of the Java Programming Gurus by Glenn Vanderburg. How does Java interface with COBOL?Leveraging existing business knowledge encapsulated within COBOL applications and accessing existing business information stored in COBOL data are two reasons for interfacing your Java application with COBOL. It is often infeasible to modify existing COBOL applications to work with Java applications. It is therefore necessary to create a glue COBOL routine to sit between the C stub routine and the existing COBOL code. For an example of this, please see Java and COBOL by Tony Hassitt, in the November 1996 IBM COBOL Newsletter. I will illustrate the techniques for interfacing a Java application with COBOL through a COBOL utility routine written to retrieve records from an existing COBOL indexed file. This example was written on a machine running Windows 95, VisualAge for COBOL for OS/2 1.2 Refresh (with support for Windows 95 and NT 3.5.1), VisualAge for C++ for Windows 3.5, and the Java Development Toolkit (JDK) 1.1 Beta 3. Step 1. Define Java class containing Native methodListing 1 defines a Java class EmpList with only one method, List_Employees. This method will return an integer value as a result and takes an array of byte data. In Java, an array is always passed by reference. This array, work_record, is actually a buffer that will be used to return a set number of records from a COBOL indexed file. Since a COBOL character is one byte in length as contrasted with a Java unicode character which is two bytes long, we will use a Java byte primitive data type to map to a COBOL character. The static call to loadLibrary loads a dynamic load library with only the library name specified, in this case emplist. On the Windows 95 platform, this will be resolved to emplist.dll. Java expects the code definition for the native method List_Employees to be contained here. class EmpList {
public native int List_Employees(byte[] work_record);
static {
System.loadLibrary("emplist");
}
}
Listing 1. EmpList.java
Step 2. Create C stubUse the javah utility to generate the C stub needed to map the Java structures to the COBOL program. The following command was used for the example. javah -classpath C:\jdk11\java\lib\classes.zip;. -stubs EmpList This will generate the EmpList.c file. Compile EmpList.c with no modification to produce the emplist.obj file. This will be used when we link the COBOL application to create the DLL. The following command was used for the example. icc -c /Mc /q+ -IC:\jdk11\java\include -IC:\jdk11\java\include\win32 /Ge- EmpList.c Step 3. Create COBOL method definitionSee Listing 3 for this discussion. The program-id is "EmpList_List_Employees", which is the method name that Java expects. The format is classname_methodname, where in our example the Java class name is EmpList and the native method name is List_Employees. You can also issue the javah command without the -stubs option to generate the C header file; this contains an extern to be used as the program-id. Every non-static Java method has a this reference which is an implicit parameter to the method; thus, the reference to this in the Procedure Division. It is there as a required place holder, we will do nothing more with it. Since all Java arrays are passed by references, we need to establish addressability within the COBOL program to the array of byte data. We accomplish this by defining a pointer LS-Emp-Reference and the work area LS-Emp-Record within the Linkage Section. Of these two, LS-Emp-Reference is the only one referenced in the Procedure Division header. We establish addressability with the following SET statement. Set Address of LS-Emp-Record to LS-Emp-Reference Note also that LS-Result as defined occupies 4 bytes which maps to a Java int primitive data type. Finally, the program has to be compiled with the PGMNAME(MIXED) option to prevent the program-id from being folded to upper case. For this example, the following command was issued to create the ListEmp.obj file. cob2 -qentryint(cdecl) -qlib -qnoexit -qnoadata -qthread -c ListEmp.cbl Since this program will be called by the EmpList C stub, the calling conventions will be cdecl; therefore, we need to tell the compiler to expect this with the entryint(cdecl) option. Step 4. Create the Dynamic Link Library (DLL)This is actually the final step in creating the interface. We packaged everything together and created the DLL that the EmpList.java program requires. The following command was used for the example. cob2 -dll EmpList.obj ListEmp.obj C:\jdk11\java\lib\javai.lib Note that aside from EmpList.obj (which must be first) and ListEmp.obj, we also need to link with the Java interpreter library (javai.lib) to form the emplist.dll. Step 5. Create a Java client programNow that we have created the emplist.dll, let us get a return on our investment by using it from a Java client program. Listing 4 shows an example of a "batch" Java program and listing 5 shows an example of a Java applet. Note that Java2COBOLApplet.java will only work with JDK 1.1 because it uses the delegation-based event model in AWT that was introduced at the JDK 1.1 level. See figure 1 for a sample of the applet running. What's Next?Now that wasn't too hard, was it? It may appear to be tedious, but you can automate this process with a makefile. Listing 6 shows a sample makefile. So what's next? Try the example out yourself. Listing 7 shows the COBOL program used to create the COBOL indexed file from the seed file shown in listing 8. If you use the makefile in Listing 6, you can issue the following command to create the EmpFile.dat file: nmake seedit References[Hassitt] Hassitt, Tony, Java and COBOL, IBM COBOL Newsletter, Issue 5, November 1996. [Vanderburg] Vanderburg, Glenn, et. al., Tricks of the Java Programming Gurus, Sams.net Publishing, 1996. ISBN 1-57521-102-5. Enjoy the article? Subscribe to Eye on Objects! |
Wilbert Kho is an advisory developer and a member of the Worldwide AD Sales and Technical Support team of IBM. He has a B.S. in Electrical Engineering from the University of the Philippines, an M.S. in Computer Science from Northern Illinois University, and an M.B.A. from the University of Phoenix. He is an adjunct faculty member of the University of Phoenix. Wilbert has been in the IT industry for more than 15 years and has been working with object technology for the past three years. |
|
![]() |