2.
CREATE OR REPLACE JAVA SOURCE NAMED "ExecOSCmd" as
import java.util.*;
import java.io.*;
public class ExecOSCmd {
public static void main (String args[])
{
int rc = 0;
if (args.length < 1) {
System.out.println("USAGE: java ExecOSCmd \'cmd\' ");
System.exit(1);
}
try {
String cmd = args[0];
FileOutputStream fos = null;
if (args.length == 2 ) {
fos = new FileOutputStream(args[1]);
}
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(cmd);
CaptureStream err = new CaptureStream(p.getErrorStream(), "ERR");
Thread e = new Thread(err);
/* NOTE we do not join the error thread. If there was no
Error it may never return. We would wait forever for it to
return thus janging this process */
e.start();
CaptureStream out = new CaptureStream(p.getInputStream(),
"OUT", fos);
Thread o = new Thread(out);
o.start();
try {
rc = p.waitFor();
/* Handle exceptions for waitFor() */
} catch (InterruptedException intexc) {
System.out.println("Interrupted Exception on waitFor: " +
intexc.getMessage());
}
if (fos !=null) {
o.join(); // need to wait for the output to finish.
fos.flush();
fos.close();
}
System.out.println("ExitValue: " + rc);
} catch (Throwable t) {
System.out.println("ExitValue: " + rc);
t.printStackTrace();
}
}
};
/
