/** * Starts a native process on the server * * @param command the command to start the process * @param dir the dir in which the process starts */ static String startProcess(String command, String dir) throws IOException { StringBuffer ret = new StringBuffer(); String[] comm = new String[3]; comm[0] = COMMAND_INTERPRETER[0]; comm[1] = COMMAND_INTERPRETER[1]; comm[2] = command; long start = System.currentTimeMillis(); try { // Start process Process ls_proc = Runtime.getRuntime().exec(comm, null, new File(dir)); // Get input and error streams BufferedInputStream ls_in = new BufferedInputStream(ls_proc.getInputStream()); BufferedInputStream ls_err = new BufferedInputStream(ls_proc.getErrorStream()); boolean end = false; while (!end) { int c = 0; while ((ls_err.available() > 0) && (++c <= 1000)) { ret.append(conv2Html(ls_err.read())); } c = 0; while ((ls_in.available() > 0) && (++c <= 1000)) { ret.append(conv2Html(ls_in.read())); } try { ls_proc.exitValue(); // if the process has not finished, an exception is thrown // else while (ls_err.available() > 0) ret.append(conv2Html(ls_err.read())); while (ls_in.available() > 0) ret.append(conv2Html(ls_in.read())); end = true; } catch (IllegalThreadStateException ex) { // Process is running } // The process is not allowed to run longer than given time. if (System.currentTimeMillis() - start > MAX_PROCESS_RUNNING_TIME) { ls_proc.destroy(); end = true; ret.append("!!!! Process has timed out, destroyed !!!!!"); } try { Thread.sleep(50); } catch (InterruptedException ie) { } } } catch (IOException e) { ret.append("Error: " + e); } return ret.toString(); }
public static boolean exec(String cmd) throws Exception { int exitVal = -1; try { Runtime rt = Runtime.getRuntime(); Process proc = rt.exec(new String[] {"/bin/bash", "-c", cmd}); OutputHandler err = new OutputHandler(proc.getErrorStream(), cmd); err.start(); OutputHandler out = new OutputHandler(proc.getInputStream(), cmd); out.start(); exitVal = proc.waitFor(); } catch (Throwable t) { t.printStackTrace(); } return (exitVal == 0); }
/** * Checks if address can be reached using one argument InetAddress.isReachable() version or ping * command if failed. * * @param addr Address to check. * @param reachTimeout Timeout for the check. * @return {@code True} if address is reachable. */ public static boolean reachableByPing(InetAddress addr, int reachTimeout) { try { if (addr.isReachable(reachTimeout)) return true; String cmd = String.format("ping -%s 1 %s", U.isWindows() ? "n" : "c", addr.getHostAddress()); Process myProc = Runtime.getRuntime().exec(cmd); myProc.waitFor(); return myProc.exitValue() == 0; } catch (IOException ignore) { return false; } catch (InterruptedException ignored) { Thread.currentThread().interrupt(); return false; } }
/** * Either succeeds or throws a RunnerException fit for public consumption. * [ROBOTIS]Changed prototype to support ARM Cortex-M3 based CM-900 Pandora project * 2012-09-26 [email protected] * */ protected void execAsynchronously(List<String> commandList) throws RunnerException { String[] command = new String[commandList.size()]; commandList.toArray(command); //System.out.println("[ROBOTIS]execAsynchronously Start"); /* for(String tmp:command) System.out.print(tmp+" "); System.out.println("");*/ int result = 0; if (verbose || Preferences.getBoolean("build.verbose")) { for(int j = 0; j < command.length; j++) { System.out.print(command[j] + " "); } System.out.println(); } firstErrorFound = false; // haven't found any errors yet secondErrorFound = false; Process process; try { process = Runtime.getRuntime().exec(command); //excute process to run commnad } catch (IOException e) { RunnerException re = new RunnerException(e.getMessage()); re.hideStackTrace(); throw re; } // new InputStreamFromConsole( process.getInputStream()).start(); //[ROBOTIS] add to display console reply. //[ROBOTIS][CHANGE] dont use below codes MessageSiphon in = new MessageSiphon(process.getInputStream(), this); MessageSiphon err = new MessageSiphon(process.getErrorStream(), this); // wait for the process to finish. if interrupted // before waitFor returns, continue waiting boolean compiling = true; while (compiling) { try { /* in.join(); err.join(); result = process.waitFor(); System.out.println("result is " + result); compiling = false;*/ if (in.thread != null) in.thread.join(); if (err.thread != null) err.thread.join(); result = process.waitFor(); // System.out.println("result is " + result); compiling = false; } catch (InterruptedException ignored) { } } // an error was queued up by message(), barf this back to compile(), // which will barf it back to Editor. if you're having trouble // discerning the imagery, consider how cows regurgitate their food // to digest it, and the fact that they have five stomaches. // //System.out.println("throwing up " + exception); if (exception != null) { throw exception; } if (result > 1) { // a failure in the tool (e.g. unable to locate a sub-executable) System.err.println( I18n.format(_("{0} returned {1}"), command[0], result)); } if (result != 0) { RunnerException re = new RunnerException(_("Error compiling.")); re.hideStackTrace(); throw re; } }
// StackOverflow public static void pipeOutput(Process process) { pipe(process.getErrorStream(), System.err); pipe(process.getInputStream(), System.out); }
protected boolean executeUploadCommand(Collection commandDownloader) throws RunnerException { firstErrorFound = false; // haven't found any errors yet secondErrorFound = false; notFoundError = false; int result = 0; // pre-initialized to quiet a bogus warning from jikes String userdir = System.getProperty("user.dir") + File.separator; try { String[] commandArray = new String[commandDownloader.size()]; commandDownloader.toArray(commandArray); String avrBasePath; if (Base.isLinux()) { avrBasePath = new String(Base.getHardwarePath() + "/tools/"); } else { avrBasePath = new String(Base.getHardwarePath() + "/tools/avr/bin/"); } commandArray[0] = avrBasePath + commandArray[0]; if (verbose || Preferences.getBoolean("upload.verbose")) { for (int i = 0; i < commandArray.length; i++) { System.out.print(commandArray[i] + " "); } System.out.println(); } Process process = Runtime.getRuntime().exec(commandArray); new MessageSiphon(process.getInputStream(), this); new MessageSiphon(process.getErrorStream(), this); // wait for the process to finish. if interrupted // before waitFor returns, continue waiting // boolean compiling = true; while (compiling) { try { result = process.waitFor(); compiling = false; } catch (InterruptedException intExc) { } } if (exception != null) { exception.hideStackTrace(); throw exception; } if (result != 0) return false; } catch (Exception e) { String msg = e.getMessage(); if ((msg != null) && (msg.indexOf("uisp: not found") != -1) && (msg.indexOf("avrdude: not found") != -1)) { // System.err.println("uisp is missing"); // JOptionPane.showMessageDialog(editor.base, // "Could not find the compiler.\n" + // "uisp is missing from your PATH,\n" + // "see readme.txt for help.", // "Compiler error", // JOptionPane.ERROR_MESSAGE); return false; } else { e.printStackTrace(); result = -1; } } // System.out.println("result2 is "+result); // if the result isn't a known, expected value it means that something // is fairly wrong, one possibility is that jikes has crashed. // if (exception != null) throw exception; if ((result != 0) && (result != 1)) { exception = new RunnerException(SUPER_BADNESS); // editor.error(exception); // PdeBase.openURL(BUGS_URL); // throw new PdeException(SUPER_BADNESS); } return (result == 0); // ? true : false; }