®
IBM Software Group
© 2006 IBM Corporation
EGL Calling Java
This Learning Module shows how to use EGL to call Java classes, methods and
access class variables.
2
EGL externalType Part
An externalType part provides the EGL mapping to a Java Class. This is similar to the mapping that an EGL Interface provides for Web Service functions, but extends to map Java class
variables and constructors.
Invoking ExternalType functions
If the class or function is marked static,
Simply invoke it using the name of its
externalType
externalType part and use dot syntax to reference the methods
typeName
typeName
.
.
methodName();
methodName();
If the class or function is non-static (more typical)
Create a variable based on its
externalType
externalType part – initialize it with a
new
new keyword
Initialize class variable valuesCall calculateTax routine
Call calculateTax routine
3
EGL Calling Java Calling EGL – 1 of 2
The following types of transfer of control are possible in EGL:
1. EGL-generated Java™ program to EGL-generated Java program
2. Non-EGL (native or hand-coded) Java program to EGL program
3. EGL-generated Java program to non-EGL Java program
4. EGL-generated Java program to DLL
5. EGL-generated Java program to .EXE or .BAT file
1. EGL-generated Java
program
program to EGL-generated Java program
Calling one EGL-generated program from another is as simple as invoking the Java class for the target
program using a call statement. Be aware, however, of package dependencies. You must invoke a class
that is one of the following:
Within the same package as the calling program.
Qualified with a package name using dot syntax.
Identified with a linkage option part.
2. Non-EGL Java program to EGL program
To invoke an EGL-generated program from a non-EGL Java program, you must do one of the following:
Access the functions by using dot syntax (library.function()) or by creating a use
declaration for the library to make its functions global to your program.
EGL-generated Java program to .EXE or .BAT file
EGL provides two functions that allow you to call a system command (such as a .bat or a
.exe executable file). These were covered as optional exercises in a previous section:
sysLib.callCmd();
–
This function transfers control to a specified executable; when the executable terminates, control returns to the
calling EGL program.
sysLib.startCmd();
–
This function transfers control to a specified executable, then keeps running; both the EGL program and the
executable run at the same time.
Related concepts – See product help
Transfer of control across programs
Linkage options part
Related tasks – See product help
Generating EGL and Web services
Generating Java wrappers
5
After finishing that, we’ll do two additional things:
Learn how to call Java API’s through the EGL JavaLib (built-in system) functions
Pass string arrays back and forth between EGL and Java
Important Note:
Important Note: Java is a case-sensitive language (okay we said that already). As such, be careful naming your classes and all the other files you
create in this workshop, as the copy/paste code will pre-determine your part names.
7
Create a new Java Package and Class
From Project Explorer:
From Project Explorer:
Right-click over \Java Resources: src\ and
create a
package
package named:
egljava
egljava
Right-click over
Right-click over
egljava
egljava and create a new
Java class named:
HelloEGLworld
HelloEGLworld
Ctrl/S
Ctrl/S (why)?
•
Ctrl/G
Ctrl/G (why)?
9
Workshop continued – Debug the EGL Program and Java Calls
Add a Breakpoint to the
callJava.egl
callJava.egl
program
From Project Explorer, right-click over the \EGLSource\programs\callJava.egl select
Debug EGL Program…
Debug EGL Program…
Step through the code
Note the variable
values before and
after the calls
to the Java Class!
10
Additional Info on External Types
When creating external types, you may
run into a Java
Java (terms and concepts)
Calling Java using the externalType Part
Calling Java using
Calling Java using
JavaLib
JavaLib
Calling COBOL Programs
Calling RPG Programs
Sub-Topics:
EGL and Remote Programs
EGL and Remote Programs
12
EGL’s JavaLib Built-in System Function
If you are not calling a custom Java Class, but only calling a
static Java API
static Java API (think of these as
Java library “built-in-functions”) – you can dispense with having to define a new externalType,
and call the API using the
JavaLib
JavaLib Built-in Function.
Here are the JavaLib
function calls
We are interested in:
You specify which package.sub-package.class, etc.
And the method that returns the Java object into the temporary variable
14
Workshop - EGL’s JavaLib invoke() and store() Functions
From Project Explorer, edit the \EGLSource\programs\callJava.egl
Towards the end of the program (as shown here in the screen capture) insert the following two functions (found in the Notes
section of this slide)
Un-comment the calls to these functions in the
main()
main() function
With your mouse cursor
inside the source,
press:
•
Ctrl/S
Ctrl/S
•
Ctrl/G
Ctrl/G
15
Workshop continued – Debug the EGL Program and Java Calls
From Project Explorer, right-click over the \EGLSource\programs\callJava.egl
select Debug EGL Program and once again, step through the code
Ctrl/S
Ctrl/S
Ctrl/G
Ctrl/G
18
OPTIONAL Workshop – Passing a String Array – Create a new JSP Page
From
\WebContent\
\WebContent\ – As you’ve done before, create a new JSP page, named:
callJavaPage
callJavaPage
For the page’s JSFHandler, add the following code (which you can copy from the Notes section below)
Notes:
Notes:
Import
Import statement
– provides reference-ability to the externalType definition
– provides reference-ability to the externalType definition
Two fixed-length string arrays
Two fixed-length string arrays
– one initialized with values (to be passed into the Java Class), one empty that will contain data returned from the Java Class call
– one initialized with values (to be passed into the Java Class), one empty that will contain data returned from the Java Class call
Variable for the externalType
Variable for the externalType