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.








You must be logged in to view or make comments on this article