package examples;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class ExecDemo {
static public String[] runCommand(String cmd)
throws IOException {
ArrayList list = new ArrayList();
Process proc = Runtime.getRuntime().exec(cmd);
InputStream istr = proc.getInputStream();
BufferedReader br =
new BufferedReader(new InputStreamReader(istr));
String str;
while ((str = br.readLine()) != null)
list.add(str);
try {
proc.waitFor();
} catch (InterruptedException e) {
System.err.println("process was interrupted");
}
if (proc.exitValue() != 0)
System.err.println("exit value was non-zero");
br.close();
return (String[]) list.toArray(new String[0]);
}
public static void main(String args[]) throws IOException {
try {
String outlist[] = runCommand("test");
for (int i = 0; i < outlist.length; i++)
System.out.println(outlist[i]);
} catch (IOException e) {
System.err.println(e);
}
}
}