/** * 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(); }
// Creates a new thread, runs the program in that thread, and reports any errors as needed. private void run(String clazz) { try { // Makes sure the JVM resets if it's already running. if (JVMrunning) kill(); // Some String constants for java path and OS-specific separators. String separator = System.getProperty("file.separator"); String path = System.getProperty("java.home") + separator + "bin" + separator + "java"; // Tries to run compiled code. ProcessBuilder builder = new ProcessBuilder(path, clazz); // Should be good now! Everything past this is on you. Don't mess it up. println( "Build succeeded on " + java.util.Calendar.getInstance().getTime().toString(), progErr); println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~", progErr); JVM = builder.start(); // Note that as of right now, there is no support for input. Only output. Reader errorReader = new InputStreamReader(JVM.getErrorStream()); Reader outReader = new InputStreamReader(JVM.getInputStream()); // Writer inReader = new OutputStreamWriter(JVM.getOutputStream()); redirectErr = redirectIOStream(errorReader, err); redirectOut = redirectIOStream(outReader, out); // redirectIn = redirectIOStream(null, inReader); } catch (Exception e) { // This catches any other errors we might get. println("Some error thrown", progErr); logError(e.toString()); displayLog(); return; } }
/* * Run the given command. */ protected static int run(String message, String[] commandArray) { try { Process process = Runtime.getRuntime().exec(commandArray, null, output); StreamProcessor.start(process.getErrorStream(), StreamProcessor.STDERR, true); StreamProcessor.start(process.getInputStream(), StreamProcessor.STDOUT, true); process.waitFor(); return process.exitValue(); } catch (IOException e) { fail(message, e); } catch (InterruptedException e) { fail(message, e); } return -1; }
public static void main(String[] args) throws Exception { int counter = 0; while (true) { Thread outThread = null; Thread errThread = null; try { // org.pitest.mutationtest.instrument.MutationTestUnit#runTestInSeperateProcessForMutationRange // *** start slave ServerSocket commSocket = new ServerSocket(0); int commPort = commSocket.getLocalPort(); System.out.println("commPort = " + commPort); // org.pitest.mutationtest.execute.MutationTestProcess#start // - org.pitest.util.CommunicationThread#start FutureTask<Integer> commFuture = createFuture(commSocket); // - org.pitest.util.WrappingProcess#start // - org.pitest.util.JavaProcess#launch Process slaveProcess = startSlaveProcess(commPort); outThread = new Thread(new ReadFromInputStream(slaveProcess.getInputStream()), "stdout"); errThread = new Thread(new ReadFromInputStream(slaveProcess.getErrorStream()), "stderr"); outThread.start(); errThread.start(); // *** wait for slave to die // org.pitest.mutationtest.execute.MutationTestProcess#waitToDie // - org.pitest.util.CommunicationThread#waitToFinish System.out.println("waitToFinish"); Integer controlReturned = commFuture.get(); System.out.println("controlReturned = " + controlReturned); // NOTE: the following won't get called if commFuture.get() fails! // - org.pitest.util.JavaProcess#destroy outThread.interrupt(); // org.pitest.util.AbstractMonitor#requestStop errThread.interrupt(); // org.pitest.util.AbstractMonitor#requestStop slaveProcess.destroy(); } catch (Exception e) { e.printStackTrace(System.out); } // test: the threads should exit eventually outThread.join(); errThread.join(); counter++; System.out.println("try " + counter + ": stdout and stderr threads exited normally"); } }
public static String[] runCommand(String cmd, File dir) throws IOException { Process p = Runtime.getRuntime().exec(cmd.split(" +"), new String[] {}, dir); String[] results = new String[] { IOUtil.readStringFully(p.getInputStream()), IOUtil.readStringFully(p.getErrorStream()) }; try { if (p.waitFor() != 0) throw new RuntimeException( "command failed [" + cmd + "]\n" + results[0] + "\n" + results[1]); } catch (InterruptedException ie) { throw new RuntimeException("uh oh"); } return results; }
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); }
@Override public void run() { String output = ""; // Get launcher jar File Launcher = new File(mcopy.getMinecraftPath() + "minecrafterr.jar"); jTextArea1.setText( "Checking for Minecraft launcher (minecrafterr.jar) in " + Launcher.getAbsolutePath() + "\n"); if (!Launcher.exists()) { jTextArea1.setText(jTextArea1.getText() + "Error: Could not find launcher!\n"); jTextArea1.setText(jTextArea1.getText() + "Downloading from Minecraft.net...\n"); try { BufferedInputStream in = new BufferedInputStream( new URL("https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft.jar") .openStream()); FileOutputStream fos = new FileOutputStream(mcopy.getMinecraftPath() + "minecrafterr.jar"); BufferedOutputStream bout = new BufferedOutputStream(fos, 1024); byte data[] = new byte[1024]; int x = 0; while ((x = in.read(data, 0, 1024)) >= 0) { bout.write(data, 0, x); } bout.close(); in.close(); } catch (IOException e) { jTextArea1.setText(jTextArea1.getText() + "Download failed..." + "\n"); } jTextArea1.setText(jTextArea1.getText() + "Download successful!" + "\n"); } // Got launcher. jTextArea1.setText(jTextArea1.getText() + "Minecraft launcher found!" + "\n"); jTextArea1.setText(jTextArea1.getText() + "Starting launcher..." + "\n"); try { System.out.println(System.getProperty("os.name")); // Run launcher in new process Process pr = Runtime.getRuntime() .exec( System.getProperty("java.home") + "/bin/java -Ddebug=full -cp " + mcopy.getMinecraftPath() + "minecrafterr.jar net.minecraft.LauncherFrame"); // Grab output BufferedReader out = new BufferedReader(new InputStreamReader(pr.getInputStream())); BufferedReader outERR = new BufferedReader(new InputStreamReader(pr.getErrorStream())); String line = ""; while ((line = out.readLine()) != null || (line = outERR.readLine()) != null) { if (!line.startsWith( "Setting user: "******"\n"; jTextArea1.setText(jTextArea1.getText() + line + "\n"); jTextArea1.setCaretPosition(jTextArea1.getText().length() - 1); } } } catch (IOException e) { jTextArea1.setText(jTextArea1.getText() + e.getMessage() + "\n"); jTextArea1.setCaretPosition(jTextArea1.getText().length() - 1); } // set output Main.Output = output; Main.SPAMDETECT = false; jTextArea1.setText(jTextArea1.getText() + "Error report complete."); jTextArea1.setCaretPosition(jTextArea1.getText().length() - 1); mcopy.analyze(); // Auto-analyze }