|
|||||||
|
|
![]() |
|
|
|
|
||
|
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
|
Editors
Contact the Editors |
| Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved. |