public String[] runJob(String command) { outputCatcher = null; try { String[] cmd = null; Process proc = null; Runtime rt = Runtime.getRuntime(); String os = getOsName(); System.out.println("OS = " + os); if (os.startsWith("Windows")) { cmd = new String[3]; cmd[0] = "cmd.exe"; cmd[1] = "/C"; cmd[2] = command; proc = rt.exec(cmd); } else if (os.startsWith("Linux")) { cmd = new String[1]; cmd[0] = command; proc = rt.exec(command.split(" ")); } System.out.println("command is:"); for (int i = 0; i < cmd.length; i++) { System.out.print(cmd[i] + " "); } System.out.println(); // any error message? errorCatcher = new StreamCatcher(proc.getErrorStream(), "ERROR"); // any output? outputCatcher = new StreamCatcher(proc.getInputStream(), "OUTPUT"); // kick them off errorCatcher.start(); outputCatcher.start(); // any error??? exitVal = proc.waitFor(); System.out.println("ExitValue: " + exitVal); if (exitVal != 0) System.out.println(errorCatcher.builder.toString()); } catch (Throwable t) { t.printStackTrace(); } // now return both the stdout and stderr streams // just use a String array for this -- not pretty but it works // stdout is at position [0], stderr is at [1] String[] outputs = new String[2]; outputs[0] = outputCatcher.builder.toString(); outputs[1] = errorCatcher.builder.toString(); return outputs; }
/** * Executes a command. * * @param command * @param args * @param path * @return * @throws IOException */ public static Output execute(String command, String[] args, String path, boolean getStatus) throws IOException { long start = System.currentTimeMillis(); ProcessBuilder builder = new ProcessBuilder(); File directory = new File(path); assert directory.exists(); assert directory.isDirectory(); // Assert.assertTrue(directory.exists(), "Directory does not exist: " + path); // Assert.assertTrue(directory.isDirectory(), "This is not a directory: " + path); builder.directory(new File(path)); if (args == null || args.length == 0) { builder.command(command); } else { List<String> cmd = new Vector<String>(); cmd.add(command); for (String arg : args) cmd.add(arg); builder.command(cmd); } _log.info( "\tRunIt::execute(..) - command: " + builder.command().toString() + "; in path: " + builder.directory()); if (Constants.DEBUG_RUNIT) { System.out.println( "\tRunIt::execute(..) - command: " + builder.command().toString() + "; in path: " + builder.directory()); } Process proc = builder.start(); // configure the streams BufferedInputStream err = new BufferedInputStream(proc.getErrorStream()); BufferedInputStream out = new BufferedInputStream(proc.getInputStream()); StreamCatcher outCatcher = new StreamCatcher(out); Thread outCatcherThread = new Thread(outCatcher); outCatcherThread.start(); StreamCatcher errCatcher = new StreamCatcher(err); Thread errCatcherThread = new Thread(errCatcher); errCatcherThread.start(); try { errCatcherThread.join(); outCatcherThread.join(); _log.info( "RunIt::execute(..) - Threads joined peacefully after: " + TimeUtility.msToHumanReadableDelta(start)); if (Constants.DEBUG_RUNIT) { System.out.println( "\t\tRunIt::execute(..) - Threads joined peacefully after: " + TimeUtility.msToHumanReadableDelta(start)); } } catch (InterruptedException e) { e.printStackTrace(); } String goodOutput = outCatcher.getOutput(); String errOutput = errCatcher.getOutput(); int exitStatus; if (getStatus) { _log.info( "Waiting for exit status of " + builder.command().toString() + "; in path: " + builder.directory()); try { exitStatus = proc.waitFor(); } catch (InterruptedException e) { _log.error( "Encountered an interrupt exception while executing " + builder.command().toString() + "; in path: " + builder.directory()); exitStatus = -1; } } else exitStatus = 0; // String output = ""; // // if (errOutput.length() > 0) { // output += "*****-START-ERROR-*****\n"; // output += errOutput; // output += "*****-END-ERROR-*****\n"; // } // // output += "*****-START-OUTPUT-*****\n"; // output += goodOutput; // output += "*****-END-OUTPUT-*****\n"; // System.out.println("\t\tRunIt::execute(..) - output: " + output); return new Output(goodOutput, errOutput, exitStatus); }