List of usage examples for java.lang Process getErrorStream
public abstract InputStream getErrorStream();
From source file:com.teradata.benchto.driver.macro.shell.ShellMacroExecutionDriver.java
private void printOutput(Process process, boolean stdoutAsError) throws IOException { logStream(process.getInputStream(), line -> { line = "stdout: " + line; if (stdoutAsError) { LOGGER.error(line);/*w w w.java 2s.c o m*/ } else { LOGGER.debug(line); } }); logStream(process.getErrorStream(), line -> LOGGER.error("stderr: " + line)); }
From source file:com.thoughtworks.go.agent.AgentProcessParentImplTest.java
@Test(timeout = 10 * 1000) //if it fails with timeout, that means stderr was not flushed -jj public void shouldLogErrorStreamOfSubprocess() throws InterruptedException, IOException { final List<String> cmd = new ArrayList<>(); Process subProcess = mockProcess(); String stdErrMsg = "Mr. Agent writes to stderr!"; when(subProcess.getErrorStream()).thenReturn(new ByteArrayInputStream(stdErrMsg.getBytes())); String stdOutMsg = "Mr. Agent writes to stdout!"; when(subProcess.getInputStream()).thenReturn(new ByteArrayInputStream(stdOutMsg.getBytes())); when(subProcess.waitFor()).thenAnswer(new Answer<Object>() { public Object answer(InvocationOnMock invocation) throws Throwable { return 42; }//ww w. j a v a 2s . co m }); AgentProcessParentImpl bootstrapper = createBootstrapper(cmd, subProcess); int returnCode = bootstrapper.run("bootstrapper_version", "bar", getURLGenerator(), new HashMap<>(), context()); assertThat(returnCode, is(42)); assertThat(FileUtils.readFileToString(stderrLog, UTF_8).contains(stdErrMsg), is(true)); assertThat(FileUtils.readFileToString(stdoutLog, UTF_8).contains(stdOutMsg), is(true)); }
From source file:eionet.gdem.utils.system.SysCommandExecutor.java
public int runCommand(String commandLine) throws Exception { /* run command */ Process process = runCommandHelper(commandLine); /* start output and error read threads */ startOutputAndErrorReadThreads(process.getInputStream(), process.getErrorStream()); // create and start a Worker thread which this thread will join for the timeout period Worker worker = new Worker(process); worker.start();/*w ww . j a v a 2s . c o m*/ try { worker.join(getTimeout()); Integer exitValue = worker.getExitValue(); if (exitValue != null) { // the worker thread completed within the timeout period return exitValue; } // if we get this far then we never got an exit value from the worker thread as a result of a timeout String errorMessage = "The command [" + commandLine + "] timed out."; LOGGER.error(errorMessage); throw new RuntimeException(errorMessage); } catch (InterruptedException ex) { worker.interrupt(); Thread.currentThread().interrupt(); throw ex; } }
From source file:Main.java
public static String runScript(String script) { String sRet = ""; try {/*w w w .j ava 2s .c o m*/ final Process m_process = Runtime.getRuntime().exec(script); final StringBuilder sbread = new StringBuilder(); Thread tout = new Thread(new Runnable() { public void run() { BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(m_process.getInputStream()), 8192); String ls_1 = null; try { while ((ls_1 = bufferedReader.readLine()) != null) { sbread.append(ls_1).append("\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } } }); tout.start(); final StringBuilder sberr = new StringBuilder(); Thread terr = new Thread(new Runnable() { public void run() { BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(m_process.getErrorStream()), 8192); String ls_1 = null; try { while ((ls_1 = bufferedReader.readLine()) != null) { sberr.append(ls_1).append("\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } } }); terr.start(); // int retvalue = m_process.waitFor(); while (tout.isAlive()) { Thread.sleep(50); } if (terr.isAlive()) terr.interrupt(); String stdout = sbread.toString(); String stderr = sberr.toString(); sRet = stdout + stderr; } catch (Exception e) { e.printStackTrace(); return null; } return sRet; }
From source file:fitnesse.testsystems.CommandRunner.java
protected void redirectOutputs(Process process, final ExecutionLogListener executionLogListener) throws IOException { InputStream stdout = process.getInputStream(); InputStream stderr = process.getErrorStream(); // Fit and SlimService new Thread(new OutputReadingRunnable(stdout, new OutputWriter() { @Override// ww w .j a v a 2s .com public void write(String output) { executionLogListener.stdOut(output); } }), "CommandRunner stdOut").start(); new Thread(new OutputReadingRunnable(stderr, new OutputWriter() { @Override public void write(String output) { executionLogListener.stdErr(output); setCommandErrorMessage(output); } }), "CommandRunner stdErr").start(); // Close stdin process.getOutputStream().close(); }
From source file:com.rhythm.louie.pbcompiler.PBCompilerMojo.java
private void execProtoCompile(List<String> args) throws IOException { getLog().info(Joiner.on(" ").join(args)); Process p = Runtime.getRuntime().exec(args.toArray(new String[args.size()])); try (InputStreamReader errorIn = new InputStreamReader(p.getErrorStream(), StandardCharsets.UTF_8); BufferedReader error = new BufferedReader(errorIn); InputStreamReader inputIn = new InputStreamReader(p.getInputStream(), StandardCharsets.UTF_8); BufferedReader input = new BufferedReader(inputIn)) { String line;/*from w w w . j a v a2s. co m*/ while ((line = error.readLine()) != null) { getLog().error(line); } while ((line = input.readLine()) != null) { getLog().info(line); } } }
From source file:edu.ucsb.eucalyptus.cloud.ws.HttpReader.java
private void getResponseToFile() { byte[] bytes = new byte[StorageProperties.TRANSFER_CHUNK_SIZE]; FileOutputStream fileOutputStream = null; BufferedOutputStream bufferedOut = null; try {/*from w w w .j ava2s . c o m*/ File outFile; File outFileUncompressed = null; if (compressed) { String outFileNameUncompressed = tempPath + File.separator + file.getName() + Hashes.getRandom(16); outFileUncompressed = new File(outFileNameUncompressed); outFile = new File(outFileNameUncompressed + ".gz"); } else { outFile = file; } httpClient.executeMethod(method); InputStream httpIn; httpIn = method.getResponseBodyAsStream(); int bytesRead; fileOutputStream = new FileOutputStream(outFile); bufferedOut = new BufferedOutputStream(fileOutputStream); while ((bytesRead = httpIn.read(bytes)) > 0) { bufferedOut.write(bytes, 0, bytesRead); } bufferedOut.close(); if (compressed) { try { Runtime rt = Runtime.getRuntime(); Process proc = rt.exec(new String[] { "/bin/gunzip", outFile.getAbsolutePath() }); StreamConsumer error = new StreamConsumer(proc.getErrorStream()); StreamConsumer output = new StreamConsumer(proc.getInputStream()); error.start(); output.start(); output.join(); error.join(); } catch (Exception t) { LOG.error(t); } if ((outFileUncompressed != null) && (!outFileUncompressed.renameTo(file))) { LOG.error("Unable to uncompress: " + outFile.getAbsolutePath()); return; } } } catch (Exception ex) { LOG.error(ex, ex); } finally { method.releaseConnection(); if (bufferedOut != null) { try { bufferedOut.close(); } catch (IOException e) { LOG.error(e); } } if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e) { LOG.error(e); } } } }
From source file:edu.wisc.doit.tcrypt.BouncyCastleFileEncrypterDecrypterIT.java
@Test public void testOpenSSLEncJavaDec() throws Exception { //Encrypt with openssl final File encryptFileScript = setupTempFile("encryptFile.sh"); encryptFileScript.setExecutable(true); final File publicKey = setupTempFile("my.wisc.edu-public.pem"); final File testFile = setupTempFile("testFile.txt"); final ProcessBuilder pb = new ProcessBuilder(encryptFileScript.getAbsolutePath(), publicKey.getAbsolutePath(), testFile.getAbsolutePath()); final Process p = pb.start(); final int ret = p.waitFor(); if (ret != 0) { final String pOut = IOUtils.toString(p.getInputStream(), TokenEncrypter.CHARSET).trim(); System.out.println(pOut); final String pErr = IOUtils.toString(p.getErrorStream(), TokenEncrypter.CHARSET).trim(); System.out.println(pErr); }/* w w w. ja va 2 s. co m*/ assertEquals(0, ret); //Decrypt with java final File encryptedFile = new File(testFile.getParentFile(), "testFile.txt.tar"); final InputStream encTestFileInStream = new FileInputStream(encryptedFile); final ByteArrayOutputStream testFileOutStream = new ByteArrayOutputStream(); fileDecrypter.decrypt(encTestFileInStream, testFileOutStream); final String actual = new String(testFileOutStream.toByteArray(), Charset.defaultCharset()).trim(); //Verify final String expected = FileUtils.readFileToString(testFile); assertEquals(expected, actual); }
From source file:Main.java
public static String runScript(String script) { String sRet = ""; try {//from www.ja v a 2 s . c o m final Process m_process = Runtime.getRuntime().exec(script); final StringBuilder sbread = new StringBuilder(); Thread tout = new Thread(new Runnable() { public void run() { BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(m_process.getInputStream()), 8192); String ls_1 = null; try { while ((ls_1 = bufferedReader.readLine()) != null) { sbread.append(ls_1).append("\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } } }); tout.start(); final StringBuilder sberr = new StringBuilder(); Thread terr = new Thread(new Runnable() { public void run() { BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(m_process.getErrorStream()), 8192); String ls_1 = null; try { while ((ls_1 = bufferedReader.readLine()) != null) { sberr.append(ls_1).append("\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } } }); terr.start(); int retvalue = m_process.waitFor(); while (tout.isAlive()) { Thread.sleep(50); } if (terr.isAlive()) terr.interrupt(); String stdout = sbread.toString(); String stderr = sberr.toString(); sRet = stdout + stderr; } catch (Exception e) { e.printStackTrace(); return null; } return sRet; }
From source file:edu.wisc.doit.tcrypt.BouncyCastleFileEncrypterDecrypterIT.java
@Test public void testJavaEncOpenSSLDec() throws Exception { //Encrypt with Java final File testFile = setupTempFile("testFile.txt"); InputStream testFileInStream = new FileInputStream(testFile); final File encFile = this.testFolder.newFile("testFile.txt.tar"); this.fileEncrypter.encrypt("testFile.txt", (int) testFile.length(), testFileInStream, new FileOutputStream(encFile)); //Decrypt with OpenSSL final File decryptFileScript = setupTempFile("decryptFile.sh"); decryptFileScript.setExecutable(true); final File privateKey = setupTempFile("my.wisc.edu-private.pem"); final ProcessBuilder pb = new ProcessBuilder(decryptFileScript.getAbsolutePath(), privateKey.getAbsolutePath(), encFile.getAbsolutePath()); final Process p = pb.start(); final int ret = p.waitFor(); if (ret != 0) { final String pOut = IOUtils.toString(p.getInputStream(), TokenEncrypter.CHARSET).trim(); System.out.println(pOut); final String pErr = IOUtils.toString(p.getErrorStream(), TokenEncrypter.CHARSET).trim(); System.out.println(pErr); }/*from w w w. j ava 2 s. c om*/ assertEquals(0, ret); final File decryptedFile = new File(encFile.getParentFile(), "testFile.txt"); //Verify final String expected = FileUtils.readFileToString(testFile); final String actual = FileUtils.readFileToString(decryptedFile); assertEquals(expected, actual); }