Troubleshooting
Problem
This example shows how an ILE RPG program would call a Java class called HelloWorld.
Resolving The Problem
This example shows how an ILE RPG program would call a method in a Java class called HelloWorld.
Caution: This is an example only. IBM accepts no responsibility for its correctness. |
1. Create an IFS file with CCSID 1208 called HelloWorld.java
One way to do this is to use the TOUCH, CHGATR, and EDTF commands. Change "mydir" to your own directory name:
QSH CMD('touch /home/mydir/HelloWorld.java')
CHGATR OBJ('/home/mydir/HelloWorld.java') ATR(*CCSID) VALUE(1208)
2. Paste the code for the Java class HelloWorld.java found near the end of this document. You can use the EDTF command to edit the file:
EDTF STMF('/home/mydir/HelloWorld.java')
3. Compile the Java source into a Java class:
CD '/home/mydir'
QSH CMD('javac -g HelloWorld.java')
4. Compile the RPG program using the source for HELLO.RPGLE near the end of this document. Using CRTBNDRPG to compile the program.
5. Ensure that Java can find your Java class using the CLASSPATH environment variable:
If the HelloWorld.class object is in IFS directory /home/mydir, you must ensure the CLASSPATH environment variable is set to this directory. This allows the JVM to find the class object. For example:
ADDENVVAR ENVVAR(CLASSPATH) VALUE('/home/mydir') LEVEL(*JOB)
6. Call the RPG program.
- If you submit the call to batch, you can see the Java output in a spool file called QPRINT. Specify CPYENVVAR(*YES) for the SBMJOB command to ensure that the new job has the CLASSPATH environment variable.
- If you call the RPG program in an interactive job, you have to do some extra work to show the Java output, which is sent to the "standard output". Compile the RPG code in DSPSTDOUT.RPGLE at the end of this document and call that program to show everything that was sent to standard output in your job. It will show the most recent output first, and you can scroll up to see earlier output.
Java and RPG code for this example:
- HelloWorld.java: The Java code for the HelloWorld class:
class HelloWorld
{
public static void Hello ()
{
System.out.println("Hello World") ;
}
}
- HELLO.RPGLE: The ILE RPG code call the Hello method in the Java class HelloWorld:
ctl-opt dftactgrp(*no);
// Prototype for Java method "Hello" in Java class "HelloWorld"
dcl-pr sayHello extproc(*JAVA
: 'HelloWorld'
: 'Hello')
static;
end-pr;
// Call the Java method
sayHello ();
return;
- DSPSTDOUT.RPGLE: ILE RPG code to display the standard output. Create this program using CRTBNDRPG if you need it.
CTL-OPT ACTGRP(*NEW);
DCL-PR printf EXTPROC(*DCLCASE);
p POINTER VALUE OPTIONS(*STRING : *NOPASS);
END-PR;
DCL-PR getchar INT(10) EXTPROC(*DCLCASE) END-PR;
DCL-C EOL x'15';
printf (EOL);
getchar ();
return;
[{"Type":"MASTER","Line of Business":{"code":"LOB68","label":"Power HW"},"Business Unit":{"code":"BU070","label":"IBM Infrastructure"},"Product":{"code":"SWG60","label":"IBM i"},"ARM Category":[{"code":"a8m0z0000000CHtAAM","label":"Programming ILE Languages"}],"ARM Case Number":"","Platform":[{"code":"PF012","label":"IBM i"}],"Version":"All Versions"}]
Historical Number
524277768
Was this topic helpful?
Document Information
More support for:
IBM i
Component:
Programming ILE Languages
Software version:
All Versions
Operating system(s):
IBM i
Document number:
634583
Modified date:
09 December 2024
UID
nas8N1012950
Manage My Notification Subscriptions