Posted By:
michael_dean
Posted On:
Thursday, November 7, 2002 12:55 PM
You have to capture the output and do something with it. To capture the script's standard output, call getInputStream() (to get an InputStream that reads from the process's standard output stream--there's also a getOutputStream() and a getErrorStream() for writing to standard input and reading from standard error, respectively) on the Process reference returned by the call to the Runtime's exec(...) methods.
For example, to echo the standard output from a script file called ./test.sh:
#!/bin/sh
echo This is some output.
echo This is more output.
echo
echo 'This is a test.'
echo 'This is only a test.'
echo Had this been a real emergency...
to the standard output of the Java application, execute the following Java application:
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
try {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("./test.sh");
InputStream is = p.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.err.println(e);
e.printStackTrace();
}
}
}