How do I run another application from Java?
Author: Deron Eriksson
Description: This Java tutorial describes using Runtime's exec method to run another application from Java.
Tutorial created using:
Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1
(Continued from page 1) The previous example was very trivial. The next example is slightly more advanced. We'll create a TestOutput class with a main method that outputs a line to standard output and a line to standard error. Following that, we'll create a RuntimeExecTest2 class that will run our TestOutput class by starting it as a process, and then RuntimeExecTest2 will read the standard output and the standard error from TestOutput, and then output those results. The TestOutput class is shown below. TestOutput.javapackage test; public class TestOutput { public static void main(String[] args) { System.out.println("This is output to standard output"); System.err.println("This is output to standard error"); } } If we execute TestOutput, we see the following result in the console. ![]() Next, we'll look at the RuntimeExecTest2 class. It calls the exec method with "java -classpath C:\\projects\\workspace\\testing\\bin test.TestOutput". We need to set the classpathW so that 'java' can find the directory where our TestOutput class is located. Once again, this example requires 'java' to be on the system path. The exec call executes the TestOutput class, and the standard output from TestOutput is read (which here is an input stream since we are reading it... the output from TestOutput is the input to RuntimeExecTest2). Additionally, the standard error from TestOutput is read by RuntimeExecTest2. RuntimeExecTest2.javapackage test; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class RuntimeExecTest2 { public static void main(String[] args) { try { Runtime runTime = Runtime.getRuntime(); Process process = runTime.exec("java -classpath C:\\projects\\workspace\\testing\\bin test.TestOutput"); InputStream inputStream = process.getInputStream(); InputStreamReader isr = new InputStreamReader(inputStream); InputStream errorStream = process.getErrorStream(); InputStreamReader esr = new InputStreamReader(errorStream); int n1; char[] c1 = new char[1024]; StringBuffer standardOutput = new StringBuffer(); while ((n1 = isr.read(c1)) > 0) { standardOutput.append(c1, 0, n1); } System.out.println("Standard Output: " + standardOutput.toString()); int n2; char[] c2 = new char[1024]; StringBuffer standardError = new StringBuffer(); while ((n2 = esr.read(c2)) > 0) { standardError.append(c2, 0, n2); } System.out.println("Standard Error: " + standardError.toString()); } catch (IOException e) { e.printStackTrace(); } } } The execution of RuntimeExecTest2 is shown below. ![]() As you can see, RuntimeExecTest2 reads the output and error streams from TestOutput and displays them. This example is neat since it shows an example of javaSW actually starting another java process, and it reads the standard output and standard error streams from that process. |