This example shows how to run a control language (CL) command from within a Java™ program.
In this example, the Java class runs a CL command. The CL command uses the Display Java Program (DSPJVAPGM) CL command to display the program that is associated with the Hello class file. The output from the CL command is in the QSYSPRT spooled file.
When you set the os400.runtime.exec system property to EXEC (which is the default), commands that you pass into the Runtime.getRuntime().exec() function use the following format:
Runtime.getRuntime()Exec("system CLCOMMAND");
where CLCOMMAND is the CL command you want to run.
Runtime.getRuntime()Exec("system \"CLCOMMAND\"");
For more information, about os400.runtime.exec and the effect it has on using java.lang.Runtime.exec(), see the following pages:
The following code assumes that you use the default value of EXEC for the os400.runtime.exec system property.
import java.io.*; public class CallCLCom { public static void main(String[] args) { try { Process theProcess = Runtime.getRuntime().exec("system DSPJVAPGM CLSF('/com/ibm/as400/system/Hello.class') OUTPUT(*PRINT)"); } catch(IOException e) { System.err.println("Error on exec() method"); e.printStackTrace(); } } // end main() method } // end class
For background information, see Use java.lang.Runtime.exec().