Newsletters Subscriptions Media Kit About Us Contact Search Home

mgo
OS/400 Edition
Volume 3, Number 57 -- August 27, 2003

Calling a Java Main Method from RPG


Hey, David:

I can successfully call Java programs from RPG using a prototyped call, but I can't get calls to a Java main method to work. My Java program is named GenerateXML, and it accepts a file name. I can run it from a QShell command line, but when I get the following message is it possible to call a main method?

Message . . . . :   Java exception received when 
   calling Java method.
Cause . . . . . :   RPG procedure GENXML in program 
   TEST/GENXML received Java exception 
      "java.lang.NoSuchMethodError: main" when 
	     calling method "main" with signature 
		    "(Ljava.lang.String;)V" in class 
			   "GenerateXML".

--Matt


From the message, it appears that you were very close. The only problem is that you did not define the String argument to main as an array. Since you have one parameter, a prototype like the following will fix your problem:

DGenerateXML$main...
D                 PR                  STATIC
D                                     EXTPROC(
D                                       *JAVA:
D                                       'GenerateXML':
D                                       'main')
D                                 O   CLASS(*JAVA:'java.lang.String')
D                                     DIM(1)
D                                     CONST

Notice that I defined the String parameter as an array with the DIM(1) keyword. Java main methods all take a varying size array of strings. If you needed two parameters to your main method, you would use DIM(2). Here is a complete program that will call the main method for a Java program named MainCall in a package named demo. In this case, I pass two parameters.

* Define prototype for Java procedure call
DMainCall$main    PR                  STATIC
D                                     EXTPROC(
D                                       *JAVA:
D                                       'demo.MainCall':
D                                       'main')
D                                 O   CLASS(*JAVA:'java.lang.String')
D                                     DIM(2)
D                                     CONST
DString$new       PR              O   EXTPROC(
D                                       *JAVA:
D                                       'java.lang.String':
D                                       *CONSTRUCTOR)
D                                     CLASS(
D                                       *JAVA:
D                                       'java.lang.String')
D bytes                         50A    CONST VARYING
Dargs             S               O   CLASS(
D                                       *JAVA:
D                                       'java.lang.String')
D                                     DIM(2)
 /free
   args(1) = String$new('Test String 1');
   args(2) = String$new('Test String 2');
   MainCall$main(args);
   *INLR = *ON
 /end-free

And here is the source for the Java program:

package demo;

public class MainCall {
    public static void main(String[] args) {
        for (int i = 0; i < args.length; i++) {
            System.out.println("Parameter " + i + " " + args[i]);
        }
    }
}

I hope this helps you make the call.

--David


Sponsored By
SUSE LINUX

Mainframe Linux for IBM
Midrange Systems

· Available for i/p/z/x Series
· Mainframe stability
· Access to legacy data

SuSE Linux Enterprise Server lets you keep your current environment and add the power of Linux. Utilize the best of both worlds, exchange data between each OS, all on one system.

Get and evaluation copy today
www.suse.com


THIS ISSUE
SPONSORED BY:

Advanced Systems Concepts
SuSE Linux


BACK ISSUES

TABLE OF
CONTENTS

Mimic Green-Screen Interactive SQL Without Development Kit

Calling a Java Main Method from RPG

Reader Feedback and Insights: He Learned Java


Editors
Howard Arner
Joe Hertvik
Ted Holt
David Morris

Managing Editor
Shannon Pastore

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.