This example shows how to call another Java™ program with java.lang.Runtime.exec(). This class calls the Hello program that is shipped as part of the IBM® Developer Kit for Java. When the Hello class writes to System.out, this program gets a handle to the stream and can read from it.
Example 1: CallHelloPgm class
import java.io.*; public class CallHelloPgm { public static void main(String args[]) { Process theProcess = null; BufferedReader inStream = null; System.out.println("CallHelloPgm.main() invoked"); // call the Hello class try { theProcess = Runtime.getRuntime().exec("java com.ibm.as400.system.Hello"); } catch(IOException e) { System.err.println("Error on exec() method"); e.printStackTrace(); } // read from the called program's standard output stream try { inStream = new BufferedReader( new InputStreamReader( theProcess.getInputStream() )); System.out.println(inStream.readLine()); } catch(IOException e) { System.err.println("Error on inStream.readLine()"); e.printStackTrace(); } } // end method } // end class
For background information, see Use java.lang.Runtime.exec().