public InputStream getErrorStream() { return process.getErrorStream(); }
private void close(Process p) { try { IOUtils.closeQuietly(p.getInputStream()); IOUtils.closeQuietly(p.getOutputStream()); IOUtils.closeQuietly(p.getErrorStream()); } finally { if (p != null) { p.destroy(); } } }
private static void closeStreams(@Nullable Process process) { if (process != null) { IOUtils.closeQuietly(process.getInputStream()); IOUtils.closeQuietly(process.getOutputStream()); IOUtils.closeQuietly(process.getErrorStream()); } }
private void generateHFile (FileDescriptor file) throws Exception { String className = getFullyQualifiedClassName(file); String command = "javah -classpath " + classpath + " -o " + jniDir.path() + "/" + className + ".h " + className; Process process = Runtime.getRuntime().exec(command); process.waitFor(); if (process.exitValue() != 0) { System.out.println(); System.out.println("Command: " + command); InputStream errorStream = process.getErrorStream(); int c = 0; while ((c = errorStream.read()) != -1) { System.out.print((char)c); } } }
private void generateHFile (FileDescriptor file) throws Exception { String className = getFullyQualifiedClassName(file); String command = "javah -classpath " + classpath + " -o " + jniDir.path() + "/" + className + ".h " + className; Process process = Runtime.getRuntime().exec(command); process.waitFor(); if (process.exitValue() != 0) { System.out.println(); System.out.println("Command: " + command); InputStream errorStream = process.getErrorStream(); int c = 0; while ((c = errorStream.read()) != -1) { System.out.print((char)c); } } }
public static InputStream getBZip2PipedInputStream(String filename) throws IOException { String bzcat = System.getProperty("bzcat", "bzcat"); Runtime rt = Runtime.getRuntime(); String cmd = bzcat + " " + filename; //log.info("getBZip2PipedInputStream: Running command: "+cmd); Process p = rt.exec(cmd); Writer errWriter = new BufferedWriter(new OutputStreamWriter(System.err)); StreamGobbler errGobbler = new StreamGobbler(p.getErrorStream(), errWriter); errGobbler.start(); return p.getInputStream(); }
private static StringBuilder captureOutput(Process process) throws IOException, InterruptedException { BufferedReader output = new BufferedReader(new InputStreamReader(process.getInputStream())); BufferedReader error = new BufferedReader(new InputStreamReader(process.getErrorStream())); StringBuilder result = new StringBuilder(); result.append("output:\n"); dump(output, result); result.append("error:\n"); dump(error, result); process.waitFor(); return result; }
public ProcessOutputStream(ProcessBuilder builder, Writer output, Writer error) throws IOException { this.process = builder.start(); errWriterThread = new StreamGobbler(process.getErrorStream(), error); outWriterThread = new StreamGobbler(process.getInputStream(), output); errWriterThread.start(); outWriterThread.start(); }
private void startPython(String tmpPath, String args) throws IOException { String pythonBinaryPath = config.getString(PythonOptions.PYTHON_BINARY_PATH); try { Runtime.getRuntime().exec(pythonBinaryPath); } catch (IOException ignored) { throw new RuntimeException(pythonBinaryPath + " does not point to a valid python binary."); } process = Runtime.getRuntime().exec(pythonBinaryPath + " -B " + tmpPath + FLINK_PYTHON_PLAN_NAME + args); new Thread(new StreamPrinter(process.getInputStream())).start(); new Thread(new StreamPrinter(process.getErrorStream())).start(); server = new ServerSocket(0); server.setSoTimeout(50); process.getOutputStream().write("plan\n".getBytes(ConfigConstants.DEFAULT_CHARSET)); process.getOutputStream().flush(); }
public BZip2PipedOutputStream(String filename, OutputStream err) throws IOException { String bzip2 = System.getProperty("bzip2", "bzip2"); String cmd = bzip2; // + " > " + filename; //log.info("getBZip2PipedOutputStream: Running command: "+cmd); ProcessBuilder pb = new ProcessBuilder(); pb.command(cmd); this.process = pb.start(); this.filename = filename; OutputStream outStream = new FileOutputStream(filename); errWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(err))); outGobbler = new ByteStreamGobbler("Output stream gobbler: " + cmd + " " + filename, process.getInputStream(), outStream); errGobbler = new StreamGobbler(process.getErrorStream(), errWriter); outGobbler.start(); errGobbler.start(); }
public static void main(String[] args) throws Exception { Process p = Runtime.getRuntime().exec("cmd /c dir"); inheritIO(p.getInputStream(), System.out); inheritIO(p.getErrorStream(), System.err); } private static void inheritIO(final InputStream src, final PrintStream dest) { new Thread(new Runnable() { public void run() { Scanner sc = new Scanner(src); while (sc.hasNextLine()) { dest.println(sc.nextLine()); } } }).start(); }
ProcessWrapper(Process process, String processTag, String command, ConsoleOutputStreamConsumer consumer, String encoding, String errorPrefix) { this.process = process; this.processTag = processTag; this.command = command; this.startTime = System.currentTimeMillis(); this.processOutputStream = StreamPumper.pump(process.getInputStream(), new OutputConsumer(consumer), "", encoding); this.processErrorStream = StreamPumper.pump(process.getErrorStream(), new ErrorConsumer(consumer), errorPrefix, encoding); this.processInputStream = new PrintWriter(new OutputStreamWriter(process.getOutputStream())); }
public OutputStream call() throws IOException { Process p = Runtime.getRuntime().exec(cmd, Util.mapToEnv(inherit(envOverrides)), workDir == null ? null : new File(workDir)); List<String> cmdLines = Arrays.asList(cmd); new StreamCopyThread("stdin copier for remote agent on "+cmdLines, p.getInputStream(), out.getOut()).start(); new StreamCopyThread("stderr copier for remote agent on "+cmdLines, p.getErrorStream(), err).start(); // TODO: don't we need to join? return new RemoteOutputStream(p.getOutputStream()); }
/** * Interrupts a thread and closes process streams. */ @Override public void interrupt() { super.interrupt(); // Close all Process streams to free allocated resources, see http://kylecartmell.com/?p=9. U.closeQuiet(proc.getErrorStream()); U.closeQuiet(proc.getInputStream()); U.closeQuiet(proc.getOutputStream()); } }
public void closeStreams() { closeQuietly(process.getInputStream()); closeQuietly(process.getOutputStream()); closeQuietly(process.getErrorStream()); }
public static void exec(String[] cmd) throws BrutException { Process ps = null; int exitValue = -99; try { ProcessBuilder builder = new ProcessBuilder(cmd); ps = builder.start(); new StreamForwarder(ps.getErrorStream(), "ERROR").start(); new StreamForwarder(ps.getInputStream(), "OUTPUT").start(); exitValue = ps.waitFor(); if (exitValue != 0) throw new BrutException("could not exec (exit code = " + exitValue + "): " + Arrays.toString(cmd)); } catch (IOException ex) { throw new BrutException("could not exec: " + Arrays.toString(cmd), ex); } catch (InterruptedException ex) { throw new BrutException("could not exec : " + Arrays.toString(cmd), ex); } }
private Process getMockedProcess(OutputStream outputStream) { Process process = mock(Process.class); when(process.getErrorStream()).thenReturn(mock(InputStream.class)); when(process.getInputStream()).thenReturn(mock(InputStream.class)); when(process.getOutputStream()).thenReturn(outputStream); return process; }
private Process mockProcess(final InputStream outputStream, final InputStream errorStream, final OutputStream inputStream) throws InterruptedException { final Process subProcess = mock(Process.class); when(subProcess.waitFor()).thenReturn(42); when(subProcess.getInputStream()).thenReturn(outputStream); when(subProcess.getErrorStream()).thenReturn(errorStream); when(subProcess.getOutputStream()).thenReturn(inputStream); return subProcess; }
/** * Executes a process and returns the process output and exit code. */ public static ProcessResult run(Process process) throws IOException, InterruptedException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream(), baos, "out>"); StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), baos, "err>"); outputGobbler.start(); errorGobbler.start(); int result = process.waitFor(); outputGobbler.waitFor(); errorGobbler.waitFor(); return new ProcessResult(result, baos.toString()); }
public ProcessStreamHandler( Process process, boolean quiet, String prefix, StreamExceptionHandler failureHandler, PrintStream out, PrintStream err ) { this.process = process; this.out = new Thread( new StreamConsumer( process.getInputStream(), out, quiet, prefix, failureHandler ) ); this.err = new Thread( new StreamConsumer( process.getErrorStream(), err, quiet, prefix, failureHandler ) ); }