Now that you know that RPG IV can call a Java class, you may be thinking, "So what? What does that do for me?" The answer is "Plenty!" There are literally tens of thousands of handy Java classes already out there, just waiting for you to begin using them. And if you can't find one that fits your needs exactly, you can write your own! You don't even have to start from scratch. One of the best things about Java is its ability to inherit attributes from a parent class. So if you find a class that converts Fahrenheit to Celsius but doesn't compute the wind chill factor, just base the new class you create to calculate wind chill on the Fahrenheit to Celsius class, and 90 percent of the work will have already been done for you!
Other examples of things you might do with Java include enabling your green-screen or ILE service program to retrieve the HTML source from a given Web page. There are hundreds of examples on the Internet (start with www.javasoft.com) that will retrieve, and in some cases parse, this HTML code for you. All you have to do is call that class from your RPG program. Or what if you have an application that needs to count the number of words, or tokens, in a character string? You could write an RPG routine to do this, but why bother? Java already has a great class called "BreakIterator" that will tokenize a string for you. In fact, there are thousands and thousands of classes just like the string tokenizer, already written, tested, and time-proven, that will handle most, if not all, of your programming needs. You don't have to reinvent the wheel every time you want to do something. And while calling Java from RPG isn't as simple as moving the contents of one variable to another, it also isn't all that hard either.
The example utility included here demonstrates how to interact with Java from RPG. Figure 1 shows the source for CL program COUNTTKC1.
%% use codefigurebegin %%
/********************************************************************/%%use codefigureend %%
/* */
/* To Compile: CRTBNDCL PGM(xxx/COUNTTKC1) SRCFILE(XXX/QCLSRC) + */
/* SRCMBR(COUNTTKC1) */
/* */
/* */
/********************************************************************/
/********************************************************************/
/* pgm : COUNTTKC1 */
/* */
/* Desc: Prompt user to enter string then call RPGIV program to */
/* interact with *JAVA class cntTkns to count tokens in */
/* string. */
/* */
/********************************************************************/
PGM
DCLF FILE(COUNTTOKD1)
MONMSG MSGID(CPF0000)
CHGVAR VAR(&INPUT) VALUE(' ')
CHGVAR VAR(&OUTPUT) VALUE(0)
SNDRCVF: SNDRCVF
IF COND(&IN03 *EQ '1') THEN(GOTO ENDPGM)
CHGVAR VAR(&IN50) VALUE('0')
/* Add CLASSPATH for java class cntTkns */
ADDENVVAR ENVVAR(CLASSPATH) +
VALUE('/')
/* Run the java class to count the tokens in a string */
CALL COUNTTOKNS PARM(&INPUT &OUTPUT)
CHGVAR VAR(&IN50) VALUE('1')
/* Remove CLASSPATH for java class cntTkns */
RMVENVVAR ENVVAR(CLASSPATH)
GOTO SNDRCVF
ENDPGM: ENDPGM
Figure 1: The CL program COUNTTKC1 sets up the environment for calling the RPG program and java class.
This CL prompts the user to enter a character string. It then adds an Environment variable for the CLASSPATH, telling the job where the Java class that the RPG program will call is located. In this case, the java class named 'cntTkns' is in the root ('/') directory. When you download and try out this code, you can put the class anywhere in the Integrated File System that you want. Just make sure you change the ADDENVVAR command in the CL program.
**********************************************************************%%use codefigureend %%
*
*
* To Compile:
* CRTBNDRPG PGM(xxx/COUNTTOKNS) SRCFILE(xxx/QRPGLESRC) +
* DFTACTGRP(*NO)
*
**********************************************************************
* Accept a character string, then call java class 'cntTkns' to count
* the number of tokens, or words, in the character string.
*
* Logic flow:
* -----------
* 1. Input character string needs to be converted to a java String
* Object before being passed to java class 'cntTkns'. This is
* because the 'tokenizer' method in clas 'cntTkns' is expecting
* a java String object as a parameter.
* To convert an RPGIV character string to a java String object,
* call the java class 'java.lang.String', passing it the RPGIV
* character string.
* 2. Instantiate the 'cntTkns' class. Since the routine to
* count the tokens in the String is in a java method ('tokenizer')
* we must first instantiate the 'cntTkns' class by calling its
* *CONSTRUCTOR method.
* 3. Call the 'tokenizer' method in class 'cntTkns'. Because method
* 'tokenizer' is not the *CONSTRUCTOR method, we must also pass
* the object refernce to the 'cntTkns' class (which we created
* in step 2 by instantiating the 'cntTkns' class) as the first
* parameter in the call to the 'tokenizer' method.
* 4. The number of tokens in the String will be returned in the
* variable named #Tokens.
*
**********************************************************************
H thread(*serialize)
* Declare object refernce variable and String object variable
D obj_ref S O CLASS(*JAVA:'cntTkns')
D string S O CLASS(*JAVA:'java.lang.String')
* Prototype procedure to create java String object
D newString PR O EXTPROC(*JAVA:
D 'java.lang.String':
D *CONSTRUCTOR)
D CLASS(*JAVA:'java.lang.String')
D charParm 100A CONST VARYING
* Prototype procedure to instantiate 'cntTkns' class
D cntTkns PR O
D EXTPROC(*JAVA:
D 'cntTkns':
D *CONSTRUCTOR)
* Prototype procedure to tokenize the String object
D Tokenizer PR 10i 0
D EXTPROC(*JAVA:
D 'cntTkns':
D 'tokenizer')
D stringParm LIKE(string)
D #Tokens S 10i 0
D Input_String S 100A VARYING
D Output_Value S 3 0
C *Entry Plist
C Parm Input_String
C Parm Output_Value
/Free
// Convert alphanumeric string to java String object
Eval string = newString(Input_String);
// Instantiate the 'cntTkns' class
Eval obj_ref = cntTkns();
// Call the 'tokenizer' method in class 'cntTkns'
Eval #Tokens = Tokenizer(obj_ref:string);
Eval Output_Value = #Tokens;
Eval *Inlr = *On;
/End-Free
Figure 2: Study this RPG program so that you can use it as a basis for building your own RPG-to-Java interactions.
RPG IV program COUNTTOKNS (Figure 2) converts the character string to a Java String Object, instantiates the Java class 'cntTkns', and then calls the method named 'tokenizer' in the 'cntTkns' class. Click here to find all the code you need, including the Java class 'cntTkns' and the display file used by the CL program).The RPG program includes comments to help explain what's going on and why the program is performing the steps it does. However, this may not be enough to answer all your questions, and that's understandable.
Getting RPG to interact with Java does require you to have at least a rudimentary understanding of Java. For example, you need to understand the difference between a Java class and a Java method. Here are a few of the other things you need to understand to be successful in working with RPG and Java:
- What it means to instantiate a Java class
- When and why to destroy a Java object
- The difference between Java data types and RPG IV data types
- The difference between a Java String Object and an RPG IV alphanumeric string
- When to pass variables by value and when to pass variables by reference
This isn't a comprehensive list, but it's enough to get you started. For more information, check out the article V5R1 Enhancements to RPG IV by Barbara Morris. You should also check out the V5R1 ILE RPG Programmer's Guide (SC09-2507-03) available online from IBM.
Shannon O'Donnell
Editor's Note: Special thanks to Barbara Morris at IBM for her input on this tip!
LATEST COMMENTS
MC Press Online