Integrating IBM COBOL for AIX with Java
Transcript of Integrating IBM COBOL for AIX with Java
IBM COBOL for AIX
Integrating IBM COBOL for AIX with Java
By: Gaby Baghdadi
Level: Introductory
April 2010
Integrating IBM COBOL for AIX with Java, Page 2 of 32
Copyright © 2010, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®.
Contents
IBM COBOL for AIX .................................................................................................. 1
About this Tutorial ................................................................................................... 3
Objectives .............................................................................................................. 3
Prerequisites ........................................................................................................... 3
System Requirements............................................................................................... 3
Glossary ................................................................................................................. 4
Start the Terminal Emulator to AIX System.................................................................. 5
Get Started with IBM COBOL Compiler Demo ............................................................... 5
COBOL class used with a COBOL client ........................................................................ 8
COBOL class used with a Java client ......................................................................... 10
COBOL class run using the java command ................................................................. 12
Accessing existing procedural COBOL code from Java.................................................. 15
Catching of Java exceptions from COBOL................................................................... 18
Multithreading in COBOL and Java ............................................................................ 21
Passing Java data types between a COBOL program and Java methods.......................... 24
Retrieving and setting JVM system properties in a COBOL program ............................... 27
COBOL explicitly creating a JVM, then re-using it from another COBOL program.............. 29
Conclusion ............................................................................................................ 32
Trademarks........................................................................................................... 32
Resources............................................................................................................. 32
Integrating IBM COBOL for AIX with Java, Page 3 of 32
Copyright © 2010, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®.
Before you start
About this Tutorial
This set of tutorials demonstrates the integration of COBOL with Java, available in IBM COBOL for AIX compiler. The principles demonstrated here also show how to combine existing COBOL assets with Java to modernize existing applications.
Objectives
• Use IBM COBOL Compiler for AIX and Java to discover flexibility available for integrating the two languages.
• Total time: 30 minutes
Prerequisites
• Basic COBOL programming skills • Basic Java programming skills
System Requirements
http://www-01.ibm.com/software/awdtools/cobol/aix/sysreq/
Integrating IBM COBOL for AIX with Java, Page 4 of 32
Copyright © 2010, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®.
Glossary
IBM COBOL for AIX: The IBM® COBOL for AIX allows you to use your existing COBOL code to upgrade your applications with the newest technologies. The goal of COBOL for AIX is to enable developers to leverage over 3 decades' worth of applications in new endeavors. It delivers needed COBOL function to continue integration of COBOL and Web-oriented business processes.
Integrating IBM COBOL for AIX with Java, Page 5 of 32
Copyright © 2010, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®.
Getting Started
Start the Terminal Emulator to AIX System
Figure 1 Get Started
Double click the “Launch AIX” icon on the desktop (See Figure 1) to start the character terminal to AIX system.
Get Started with IBM COBOL Compiler Demo
A successful login will result with the user presented with a menu of the demos hosted on the server. Type 3 and press the Enter key to select the “IBM COBOL with Java on AIX” demo. See Figure 2 Login Figure 2 Login
Integrating IBM COBOL for AIX with Java, Page 6 of 32
Copyright © 2010, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®.
Figure 3 Demo Prepared
Note: Starting another command window will start the demonstration setup of your
environment. This will result in loss of any work done in your home directory. This will impact any progress you have made on demo steps going forward. This demo does not require more than one terminal window. However, if you prefer more than one terminal window then you may open them before going forward.
On the terminal window, you will see important information and the directory path to the compiler invocation command (See Figure 3 Demo Prepared oval red). In your home directory there are nine sub-directories (See Figure 3 Demo Prepared rectangle red), each sub-directory contains a demonstration of a feature that can be used when integrating IBM COBOL for AIX with Java. This document steps over each demo in its own section; you may start any section in the order that you prefer. The terminal window is now ready for commands (See Figure 3 Login arrow). Your home directory contains all the necessary source code and scripts for demonstration. Type the ls command to see the directory content (See Figure 4 Contents).
Integrating IBM COBOL for AIX with Java, Page 7 of 32
Copyright © 2010, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®.
Command: ls Figure 4 Contents
These nine directories contain following demos
Demo directory Demo objective 1_cobolClass Demonstrates COBOL class used with a COBOL client. 2_cobolJava Demonstrates COBOL class used with a Java client. 3_javaCommand Demonstrates COBOL class run using the java command. 4_accessingCobol Demonstrates accessing existing procedural COBOL code from Java. 5_catchingException Demonstrates catching of Java exceptions from COBOL. 6_multithreading Demonstrates multithreading in COBOL and Java 7_passingData Demonstrates Passing Java data types between a COBOL program
and Java methods 8_setJVMproperties Demonstrates retrieving and setting JVM system properties in a
COBOL program 9_JvmCreateReuse Demonstrates COBOL explicitly creating a JVM, then re-using it from
another COBOL program Screen images in the following demo sections are recorded using makefiles to compile and link. Scripts are used to launch demo programs. Users are encouraged to try the challenge of compiling, linking and running the demo program using actual commands. Actual commands are given at the end of each step under “Hint for User Challenge” heading.
Integrating IBM COBOL for AIX with Java, Page 8 of 32
Copyright © 2010, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®.
COBOL class used with a COBOL client
Highlights:
- Program entry point is a COBOL client program ‘cblclient.cbl’ which instantiates an instance of class ‘cblclass’
- ‘cblclass’ is a class defined in COBOL ‘cblclass.cbl’ - The COBOL client then invokes a method ‘credit’ on the new ‘cblclass’ object
JNI services used: - NewGlobalRef
Steps: 1. Change directory to 1_cobolClass and list contents. See figure 5 Change directory
1_cobolClass Command: cd 1_cobolClass and ls Figure 5 Change directory 1_cobolClass
2. Compile and link cbclass.cbl and cbclient.cbl. See figure 6 Make 1_cobolClass
Command: make
Integrating IBM COBOL for AIX with Java, Page 9 of 32
Copyright © 2010, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®.
Figure 6 Make 1_cobolClass
Hint for User challenge: cob2_j -c cblclass.cbl # Generates Java program cblclass among other artifacts javac cblclass.java cob2_j -o cblclient cblclient.cbl cob2_j -o libcblclass.so -bnoentry -bexpall -bM:SRE cblclass.o
3. Run cblclient program. See figure 7 run 1_cobolClass
Command: run.ksh
Figure 7 Run 1_cobolClass
Hint for User challenge: cblclient Use more command to see programming details of COBOL client program ‘cblclient.cbl’ and Definition of COBOL class in ‘cblclass.cbl’. Hint:
more cblclient.cbl more cblclass.cbl
4. Change the current directory back to home directory
Command: cd .. This completes this section of “COBOL class used with a COBOL client”.
Integrating IBM COBOL for AIX with Java, Page 10 of 32
Copyright © 2010, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®.
COBOL class used with a Java client
Highlights:
- Program entry point is a Java class ‘javamain’. The program ‘javamain.java’ instantiates an instance of class ‘cblclass’.
- ‘cblclass’ is a class defined in COBOL ‘cblclass.cbl’ - The Java client invokes a method ‘credit’ on ‘cblclass’ object
JNI services used: - NewGlobalRef
Steps: 1. Change directory to 2_cobolJava and list contents. See figure 8 Change directory
2_cobolJava Command: cd 2_cobolJava and ls Figure 8 Change directory 2_cobolJava
2. Compile and link cbclass.cbl and cbclient.cbl. See figure 6 Make 2_cobolJava Command: make
Figure 9 Make 2_cobolJava
Integrating IBM COBOL for AIX with Java, Page 11 of 32
Copyright © 2010, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®.
Hint for User challenge: cob2_j -c cblclass.cbl # Generates Java program cblclass among other artifacts cob2_j -o libcblclass.so -bnoentry -bexpall -bM:SRE cblclass.o javac cblclass.java javac javamain.java
3. Run Java program javamain. See figure 10 run 2_cobolJava
Command: run.ksh
Figure 10 Run 2_cobolJava
Hint for User challenge: java javamain
Use more command to see programming details of Java client program ‘javamain.java’ and Definition of COBOL class in ‘cblclass.cbl’. Hint: more javamain.cbl more cblclass.cbl
4. Change the current directory back to home directory
Command: cd .. This completes this section of “COBOL class used with a Java client”.
Integrating IBM COBOL for AIX with Java, Page 12 of 32
Copyright © 2010, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®.
COBOL class run using the java command
Highlights:
- COBOL class defines a factory static method ‘main’, which enables the COBOL program to be run using the java command.
- ‘main’ method in COBOL class uses JNI array services to extract the command line arguments
- Defining and using overloaded methods in COBOL
JNI services used: - GetArrayLength - GetObjectArrayElement - GetStringChars - GetStringLength - ReleaseStringChars
Steps: 1. Change directory to 3_javaCommand and list contents. See figure 11 Change directory
3_javaCommand Command: cd 3_javaCommand and ls Figure 11 Change directory 3_javaCommand
2. Compile and link oo17.cbl. See figure 12 Make 3_javaCommand Command: make
Integrating IBM COBOL for AIX with Java, Page 13 of 32
Copyright © 2010, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®.
Figure 12 Make 3_javaCommand
Hint for User challenge: cob2_j -c oo17.cbl # Generates Java program oo17 among other artifacts cob2_j -o libcblclass.so -bnoentry -bexpall -bM:SRE oo17.o javac oo17.java
3. Run Java program oo17. See figure 13 run 3_JavaCommand
oo17.class expects three quoted string as an argument. Example of command line arguments: “aaa” “bbb” “ccc” Command: run.ksh
Integrating IBM COBOL for AIX with Java, Page 14 of 32
Copyright © 2010, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®.
Figure 13 Run 3_javaCommand
Hint for User challenge: java oo17 "aaa" "bbb" "ccc"
Use more command to see programming details of COBOL program ‘oo17.cbl’. Hint: more oo17.cbl
4. Change the current directory back to home directory
Command: cd .. This completes this section of “COBOL class run using the java command”.
Integrating IBM COBOL for AIX with Java, Page 15 of 32
Copyright © 2010, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®.
Accessing existing procedural COBOL code from Java
Highlights: The entry point is a Java class main ‘oofdriver’. ‘oofdriver’ invokes a COBOL class factory method ‘oofw.cbl’. ‘oofw.cbl’ then calls a separately compiled procedural COBOL program ‘oofw2.cbl’. ‘oofw2.cbl’ then calls another separately compiled procedural COBOL program ‘oofsub.cbl’. oofw.cbl and oofw2.cbl programs are compiled with the DYNAM compiler directive. DYNAM directive causes non-nested, separately compiled programs (oofw2 and oofsub) to be loaded for CALL dynamically at runtime (and deleted for CANCEL). No JNI services are used.
Steps: 1. Change directory to 4_accessingCobol and list contents. See figure 14 Change directory
4_accessingCobol Command: cd 4_accessingCobol and ls Figure 14 Change directory 4_accessingCobol
2. Compile and link oofdriver.java, oofsub.cbl, oofw.cbl and oofw2.cbl. See figure 15 Make 4_accessingCobol Command: make
Integrating IBM COBOL for AIX with Java, Page 16 of 32
Copyright © 2010, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®.
Figure 15 Make 4_accessingCobol
Hint for User challenge: cob2 -c oofw2.cbl # Note compiled as normal COBOL program using cob2 cob2 -o oofw2 oofw2.o cob2_j -c oofw.cbl cob2_j -o liboofw.a -bM:SRE -bnoentry -bexpall oofw.o javac oofw.java cob2 -c oofsub.cbl # Note compiled as normal COBOL program using cob2 cob2 -o OOFSUB oofsub.o javac oofdriver.java
3. Run Java program oofdriver. See figure 16 run 4_accessingCobol
Running this program requires setting COBPATH environment variable for runtime to be able to find and load other programs. Command: run.ksh
Integrating IBM COBOL for AIX with Java, Page 17 of 32
Copyright © 2010, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®.
Figure 16 Run 4_accessingCobol
Hint for User challenge: export OCOBPATH=$COBPATH # Save present value of COBPATH export COBPATH=. # Set the value of COBPATH to current directory java oofdriver # Run the java program export COBPATH=$OCOBPATH # Restore the original value of COBPATH after the run
Use more command to see programming details of oofdriver.java, oofsub.cbl, oofw.cbl and oofw2.cbl programs. Hint: more oofdriver.cbl more oofsub.cbl more oofw.cbl more oofw2.cbl
4. Change the current directory back to home directory
Command: cd .. This completes this section of “Accessing existing procedural COBOL code from Java”.
Integrating IBM COBOL for AIX with Java, Page 18 of 32
Copyright © 2010, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®.
Catching of Java exceptions from COBOL
Highlights: Java class defines these methods:
- jidivide1: divides by zero, catches the exception - jidivide2: divides by zero, catches the exception, then throws it - jidivide3: divides by zero, doesn't catch the exception
COBOL program calls JNI's ExceptionOccurred(), ExceptionDescribe(), ExceptionClear() and printStackTrace()API to retrieve details of Java exception after invoking the Java method in following manner.
- Invokes jidivide1, verifies that there are no pending exceptions - Invokes jidivide2, verifies that there is a pending exception, prints a debugging
message about the exception and clears the exception - Invokes jidivide3, verifies that there is a pending exception, prints a debugging
message about the exception and clears the exception
JNI services used: - ExceptionOccurred - ExceptionDescribe - ExceptionClear
Steps: 1. Change directory to 5_catchingException and list contents. See figure 17 Change
directory 5_catchingException Command: cd 5_catchingException and ls Figure 17 Change directory 5_catchingException
2. Compile and link jidic01driver.cbl and jidiv01j.java. See figure 18 Make 5_catchingException Command: make
Integrating IBM COBOL for AIX with Java, Page 19 of 32
Copyright © 2010, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®.
Figure 18 Make 5_catchingException
Hint for User challenge: cob2_j -c jidiv01driver.cbl cob2_j -o jidiv01driver jidiv01driver.o javac jidiv01j.java
3. Run COBOL program jidiv01driver. See figure 19 run 5_catchingException
Command: run.ksh
Figure 19 Run 5_catchingException
Integrating IBM COBOL for AIX with Java, Page 20 of 32
Copyright © 2010, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®.
Hint for User challenge: jidiv01driver
Use more command to see programming details of jidic01driver.cbl and jidiv01j.java programs. Hint: more jidic01driver.cbl more jidiv01j.java
4. Change the current directory back to home directory
Command: cd .. This completes this section of “Catching of Java exceptions from COBOL”.
Integrating IBM COBOL for AIX with Java, Page 21 of 32
Copyright © 2010, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®.
Multithreading in COBOL and Java
Highlights:
COBOL program defines a "NewThread" class as a subclass of java.lang.Thread. NewThread class defines the following factory (static) and instance methods:
Factory methods: - CreateNewThread: creates a new NewThread instance, calls its Init() method, and
returns the new instance object to the caller (Java)
Instance methods: - Init: initializes the NewThread instance data - run: overrides java.lang.Thread's run() method (called when the thread is
started)
Java class main(): - Calls NewThread.CreateNewThread() to create a number (five) of threads - Calls NewThread.start() (inherited from base java.lang.Thread), to start - each thread - Calls NewThread.join() (from base java.lang.Thread) to wait for each thread to
terminate
JNI services used: - NewGlobalRef - ExceptionOccurred - ExceptionDescribe - ExceptionClear
Steps: 1. Change directory to 6_multithreading and list contents. See figure 20 Change directory
6_multithreading Command: cd 6_multithreading and ls
Integrating IBM COBOL for AIX with Java, Page 22 of 32
Copyright © 2010, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®.
Figure 20 Change directory 6_multithreading
2. Compile and link Driver.java and NewThread.cbl. See figure 21 Make 6_multithreading Command: make
Figure 21 Make 6_multithreading
Hint for User challenge: cob2_j -c NewThread.cbl cob2_j -o libNewThread.so NewThread.o -bexpall javac Driver.java
3. Run Java program Driver.class. See figure 22 run 6_multithreading
Command: run.ksh
Integrating IBM COBOL for AIX with Java, Page 23 of 32
Copyright © 2010, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®.
Figure 22 Run 6_multithreading
Hint for User challenge: java Driver
Use more command to see programming details of Driver.java and NewThread.cbl programs. Hint: more Driver.java more NewThread.cbl
4. Change the current directory back to home directory
Command: cd .. This completes this section of “Multithreading in COBOL and Java”.
Integrating IBM COBOL for AIX with Java, Page 24 of 32
Copyright © 2010, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®.
Passing Java data types between a COBOL program and Java methods
Highlights: Demonstrates passing the following Java data types between a COBOL program and Java methods:
- boolean, byte, char, int, short, long, float, double, String
JNI services used: - NewStringUTF - GetStringUTFChars - GetStringUTFLength
Steps: 1. Change directory to 7_passingData and list contents. See figure 23 Change directory
7_passingData Command: cd 7_passingData and ls Figure 23 Change directory 7_passingData
2. Compile and link oo3c.java and oo3driver.cbl. See figure 24 Make 7_passingData Command: make
Integrating IBM COBOL for AIX with Java, Page 25 of 32
Copyright © 2010, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®.
Figure 24 Make 7_passingData
Hint for User challenge: cob2_j -c oo3driver.cbl cob2_j -o oo3driver oo3driver.o javac oo3c.java
3. Run COBOL program oo3driver. See figure 25 run 7_passingData
Command: run.ksh
Figure 25 Run 7_passingData
… …
Integrating IBM COBOL for AIX with Java, Page 26 of 32
Copyright © 2010, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®.
Hint for User challenge: oo3driver
Use more command to see programming details of oo3c.java and oo3driver.cbl programs. Hint: more oo3c.java more oo3driver.cbl
4. Change the current directory back to home directory
Command: cd .. This completes this section of “Passing Java data types between a COBOL program and Java methods”.
Integrating IBM COBOL for AIX with Java, Page 27 of 32
Copyright © 2010, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®.
Retrieving and setting JVM system properties in a COBOL program
Highlights:
- COBOL program customizes the initialization of the JVM, including defining new Java system properties, by setting COBOL's runtime environment variable COBJVMINITOPTIONS
- COBOL program then extracts the values of certain Java system properties, including
JVM's own (e.g. java.version), as well as those that were set through environment variable COBJVMINITOPTIONS
JNI services used: - GetStringChars - GetStringLength - NewString
Steps: 1. Change directory to 8_setJVMproperties and list contents. See figure 26 Change
directory 8_setJVMproperties Command: cd 8_setJVMproperties and ls Figure 26 Change directory 8_setJVMproperties
2. Compile and link oo16.cbl. See figure 27 Make 8_setJVMproperties Command: make
Integrating IBM COBOL for AIX with Java, Page 28 of 32
Copyright © 2010, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®.
Figure 27 Make 8_setJVMproperties
Hint for User challenge: cob2_j -o oo16 oo16.cbl
3. Run COBOL program oo16. See figure 28 run 8_setJVMproperties
Command: run.ksh
Figure 28 Run 8_setJVMproperties
Hint for User challenge:
export "COBJVMINITOPTIONS=-Djava.naming.factory.initial=com.ibm.websphere.naming.WsnInitialContextFactory -Dxxx=aaa(bbb,ccc,ddd) -verbose:gc" # Set environment variable oo16
Use more command to see programming details of oo16.cbl program and run.ksh script. Hint: more oo16.cbl more run.ksh
4. Change the current directory back to home directory
Command: cd ..
Integrating IBM COBOL for AIX with Java, Page 29 of 32
Copyright © 2010, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®.
This completes this section of “Retrieving and setting JVM system properties in a COBOL program”.
COBOL explicitly creating a JVM, then re-using it from another COBOL program
Highlights: COBOL program explicitly creates a JVM, calls another COBOL program that uses OO, and verifies that it is reusing the existing JVM. JNI services used:
- JNI_CreateJavaVM
Steps: 1. Change directory to 9_JvmCreateReuse and list contents. See figure 29 Change
directory 9_JvmCreateReuse Command: cd 9_JvmCreateReuse and ls Figure 29 Change directory 9_JvmCreateReuse
2. Compile and link oo2b.cbl, oo2driver.cbl and oo2c.java. See figure 30 Make 9_JvmCreateReuse Command: make
Integrating IBM COBOL for AIX with Java, Page 30 of 32
Copyright © 2010, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®.
Figure 30 Make 9_JvmCreateReuse
Hint for User challenge: cob2_j -c oo2driver.cbl cob2_j -c oo2b.cbl cob2_j -o oo2driver oo2driver.o oo2b.o javac oo2c.java
3. Run COBOL program oo16. See figure 31 run 9_JvmCreateReuse
Command: run.ksh
Figure 31 Run 9_JvmCreateReuse
Hint for User challenge:
oo2driver
Use more command to see programming details of oo2b.cbl, oo2driver.cbl and oo2c.java. Hint: more oo2b.cbl more oo2driver.cbl more oo2c.java
Integrating IBM COBOL for AIX with Java, Page 31 of 32
Copyright © 2010, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®.
4. Change the current directory back to home directory
Command: cd .. This completes this section of “COBOL explicitly creating a JVM, then re-using it from another COBOL program”.
Integrating IBM COBOL for AIX with Java, Page 32 of 32
Copyright © 2010, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®.
What you have learned In the sections above you learned how to:
• Use the IBM COBOL compiler’s Java features • Use the Java features to take advantage of old or legacy COBOL source code in
modern applications. • Enhance COBOL programs by taking advantage of new Java source code.
Conclusion
This concludes the tutorial on using the IBM COBOL for AIX compiler. This tutorial has shown how to integrate COBOL with Java to take advantage of object oriented programming with existing software assets in COBOL to modernize and combine business critical applications.
Trademarks IBM and the IBM logo are trademarks of International Business Machines Corporation in the United States, other countries or both. Microsoft, Windows, Windows NT, and the Windows logo are trademarks of Microsoft Corporation in the United States, other countries, or both. Other company, product and service names may be trademarks or service marks of others.
Resources
Community
COBOL Café http://www-949.ibm.com/software/rational/cafe/community/cobol
Learn
http://www-01.ibm.com/software/awdtools/cobol/aix/library/