21
Thu, Nov
1 New Articles

TechTip: Calling Java from RPG!

Java
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times
One of the coolest things with the new V5R1 RPG IV compiler is the ability to call Java classes as if they were native procedures. This ability is accomplished through the use of the Java Native Interface (JNI) protocols. JNI is relatively complex to use; however, RPG IV does a very good job of hiding the complexity of JNI for you, making the task of interfacing with Java much easier.

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 %%
/********************************************************************/
/* */
/* 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
%%use codefigureend %%

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.
Next, the CL program calls the RPG program COUNTTOKNS (Figure2), passing it the string the user entered and an empty numeric variable. COUNTTOKNS does all the interfacing with the Java class. When control returns to the CL program, the number of tokens, or words, in the user-entered string,which is stored in the numeric variable passed to the RPG program, is displayed on the screen.

%%use codefigurebegin %%
 **********************************************************************
*
*
* 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
%%use codefigureend %%
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
This email address is being protected from spambots. You need JavaScript enabled to view it.

Editor's Note: Special thanks to Barbara Morris at IBM for her input on this tip!

SHANNON ODONNELL
Shannon O'Donnell has held a variety of positions, most of them as a consultant, in dozens of industries. This breadth of experience gives him insight into multiple aspects of how the AS/400 is used in the real world. Shannon continues to work as a consultant. He is an IBM Certified Professional--AS/400 RPG Programmer and the author of an industry-leading certification test for RPG IV programmers available from ReviewNet.net.
 
BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: