Newsletters Subscriptions Media Kit About Us Contact Search Home

Stuff
OS/400 Edition
Volume 2, Number 9 -- April 24, 2003

Calling Java from RPG: A Refresher


by Kevin Vandever

[The code for this article is available for download.]

In the last issue I discussed some advanced features of the Java Virtual Machine related to calling Java methods from RPG. I talked about how to control creating and destroying the JVM, and why you might want to do so. I then told you that I would take you further into the world of the RPG-to-Java interface; however, I've received a number of e-mails asking how to call home-grown Java applications, so I thought I would take a moment, or an article, to walk through the steps of calling a home-grown Java application from RPG.

Three-Step Program

Even though calling home-grown Java applications is not much different from calling the core Java APIs, there are a couple of extra steps you have to take. The following are those steps necessary to successfully call you home-grown Java applications.

  1. Code and compile your application. Okay, this one seems obvious, but it is a step, nonetheless. Math2 and its associated prototype copybook, Math2PR, are the RPG members necessary to call my home-grown Java application. These are simplified versions of the Math and MathPR source members from my article "Prototyping and Calling Java Methods from RPG," and they have been modified and simplified to call my home-grown Java application to add two integers and return the sum. For information on what I'm doing in the program, please refer to that article, in which I included a detailed explanation of calling Java methods from RPG. The Java in this case is called myClass. If you take a look at the code, it's quite simple. It contains one method, addStuff, which accepts two integers and returns the sum. That's all there is to it. Now compile the RPG code, using option 14 in PDM or the directions at the top of the Math2 source member, and then compile the Java source using the javac command from the Qshell environment on your iSeries. Again, refer to the original article for details on compiling the Java source. You can place your Java source and class anywhere you want in the IFS on your iSeries. I put mine in /home/kvandever, but it doesn't matter. The important thing is that you know where you put it, so you can go to step 2.
  2. Tell the JVM where your home-grown classes are; or, as it's more aptly called, set your CLASSPATH appropriately. This is the key element necessary for calling your own Java applications. There are many ways to set the CLASSPATH environment variable on the iSeries. For a closer look at that topic, check out "How Do I Set My CLASSPATH? Let Me Count the Ways." For now, just use the following command. Assuming that you placed your Java class in /home/kvandever, you would enter the following at the AS/400 command line:
    ADDENVVAR ENVVAR(CLASSPATH) VALUE('/home/kvandever')
    
  3. Run the RPG. Yep, that's all there is to it. When your RPG program first calls the Java method, it will start the JVM with the default CLASSPATH environment variable. That default CLASSPATH will include the directory or directories that you included in the command above. I've hard-coded the integer values to keep things really simple, but if all goes well, and should all go well, the number 41 will be displayed on your screen.

The Gotchas

Okay, so things might not always go well. You may get my stuff to work, but when you attempt to call your own stuff, you may run into some difficulty. But in my experience, that difficulty will probably have less to do with CLASSPATH issues and more to do with RPG-to-Java data type issues. For that reason you have to be very careful that you use the correct data types in both the RPG and the Java. The table below is from IBM's WebSphere Development Studio ILE RPG Programmer's Guide. It contains invaluable information, and if you understand this table before you start developing, you will save yourself a lot of frustration on the back end trying to understand Java exception errors.

IBM Table

However, if you don't want to mess with this table and you'd rather tackle Java Exception errors, you can still do so, and I can help you there, too, with one simple clue. If you receive a Java exception error, place the cursor on that error and press the F1 key. This will give you more information about that error. If the error states that it had trouble calling your Java class, you probably have a CLASSPATH issue. But if the exception error states that there was trouble calling a specific method within that class, you probably have a data conversion issue. Then it might be time to go back and check out the table.

Let's Move 'Em Out

Now you're ready to call any Java application from RPG, homegrown or not. This should prepare you enough to continue on our RPG/Java interface journey. There is a lot more to see, but feel free to stop me like this from time to time for a breather or refresher. We are in no hurry.


Sponsored By
PROFOUND LOGIC SOFTWARE

Announcing RPGsp!

RPG Smart Pages (RPGsp) is an Integrated Development Environment for creating dynamic web applications.

If you are a shop with strong RPG skills, you can start developing e-Business / e-Commerce or Intranet Applications IMMEDIATELY! It's easier than traditional RPG/DDS development!

RPG Smart Pages (RPGsp) integrates an RPG aware WYSIWYG HTML Designer with RPG-Alive enabled Code Editing Capabilities, allowing you to create and compile dynamic Web applications as well as traditional RPGLE programs from one sophisticated GUI development environment!

For a Free Trial or more information, contact Profound Logic Software at www.ProfoundLogic.com


THIS ISSUE
SPONSORED BY:

T.L. Ashford
East Coast Computer
WorksRight Software
Profound Logic Software


BACK ISSUES

TABLE OF
CONTENTS
JAXB takes XML to Java

Scribble on SQL's Scratchpad

Using Pointer-Based Variables in RPG IV

Calling Java from RPG: A Refresher


Editors
Shannon O'Donnell
Kevin Vandever

Managing Editor
Shannon Pastore

Contributing Editors:
Howard Arner
Raymond Everhart
Joe Hertvik
Ted Holt
David Morris

Publisher and
Advertising Director:

Jenny Thomas

Advertising Sales Representative
Kim Reed

Contact the Editors
Do you have a gripe, inside dope or an opinion?
Email the editors:
editors@itjungle.com


Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.