I consider myself a novice, but I think my problem belongs in this forum. The following
java code (that I did not write) is stored in an Oracle database as a stored procedure and is called from a web-based application. The main use for this code is to copy files from a server to another server. It works just fine, however, I really need to capture the error code (rc). The way it is coded now, I can only see the message if I run it online in sqlplus. After reading other postings,here is my understanding that because the processing is done in the main method and it is performing operating system calls, you cannot capture an error code. Can someone verify this for me, or give me direction on how to change this so that I can capture the code.
Thanks!
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();
}
}
}