FAQ > PERCobol and Java > Please tell us how to pass values from java to the COBOL program and get values back. An example of getting some parameters, loading up an array of value objects and returning this array back to java
Be sure to first examine the Java Calling COBOL sample. Create a new
'LegacyJ PERCobol' project, call it java2cobol. In it, you'll find a .java file and
a .cbl file.
A Java program creates an instance of the COBOL program. From the Java
side, the class name will be obvious; it's the COBOL program's PROGRAMID
in all lower-case with dashes converted to underscores. (It's all lower-case
because COBOL is case insensitive.) The instance is created using the
normal 'new' operation with an empty constructor.
That instance of the program is just a normal Java object. It has a welldefined
interface called com.legacyj.api.Callable with a single method, named
'call' that takes a boolean[] and an Object[]. The boolean[] and Object[]
should be the same length; each element is a parameter. The boolean is true
for BY REFERENCE calling (actually copy/restore when calling from Java)
or false for BY CONTENT calling, and the Object is the data to be passed.
The Object must be a java.lang.Number descendent (Integer, BigDecimal, etc.),
String or byte[]. The byte[] is treated as raw COBOL memory. It's passed as
if via the COBOL MOVE verb. From the Java side, this just means it will be
in a form usable from the COBOL side. The integer will be formatted
according to whatever USAGE is in effect in the COBOL data automatically.
From the Java side, know that each COBOL data item is a full Java object,
typed according to its usage, so it's smart about moving data.)
See the PERCobol IDE's Help Contents, PERCobol Programmer's Guide,
Chapter 2 Program Lifecycle; it has a section on Java to COBOL calls and one
on COBOL to Java calls. The COBOL to Java calling section includes
JavaDoc for the Callable and Datatype interfaces. The Datatype interface is
primarily used when Cobol is CALLing Java; it allows the COBOL to pass
datatypes and Java to convert them using toInt(), toString(), toText(), etc.
This sample has the Cobol and Java in the same project, but that need not be
the case. To be in different projects, as it looks like you'd like to do:
1) In the Java project, right-click the Java project, properties, and set the Java
Build Path to depend upon the other project. That will make it available
to its CLASSPATH.
2) In the COBOL project, set its Java Build Path setting to export the
percobol.jar runtime. This is done by checking the
PERCOBOL_CORE/percobol/percobol.jar entry in the Order and Export
property tab. That will allow a Java program in the other dependent
project to successfully execute the PERCobol programs.
When deploying, the COBOL project is deployed to its own .jar file (that
includes the PERCobol runtimes, licensing and the actual application).
The Java side then just must include the deployed .jar in its classpath.
Last updated on December 28, 2009 by Daniel Myers
