Printing Runtime exec() OutputStream to console

一世执手 提交于 2019-11-26 02:57:38

问题


I am trying to get the OutputStream of the Process initiated by exec() to the console. How can this be done?

Here is some incomplete code:

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.Reader;

public class RuntimeTests
{
    public static void main(String[] args)
    {
        File path = new File(\"C:\\\\Dir\\\\Dir2\");
        String command = \"cmd /c dir\";
        Reader rdr = null;
        PrintStream prtStrm = System.out;
        try
        {
            Runtime terminal = Runtime.getRuntime();

            OutputStream rtm = terminal.exec(command, null, path).getOutputStream();
            prtStrm = new PrintStream(rtm);
            prtStrm.println();
        } 
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

回答1:


You need to start a new thread that would read terminal output stream and copy it to the console, after you call process.waitFor()




回答2:


I recently ran into this problem and just wanted to mention that since java 7 the process builder api has been expanded. This problem can now be solved with:

ProcessBuilder pb = new ProcessBuilder("yourcommand");
pb.redirectOutput(Redirect.INHERIT);
pb.redirectError(Redirect.INHERIT);
Process p = pb.start();



回答3:


I believe this is what you're looking for:

  String line;
  Process p = Runtime.getRuntime().exec(...);
  BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
  while ((line = input.readLine()) != null) {
    System.out.println(line);
  }
  input.close();



回答4:


I faced the similar problem and I am using the following code.

Process p = Runtime.getRuntime().exec(".....");
p.waitFor();

String line;

BufferedReader error = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while((line = error.readLine()) != null){
    System.out.println(line);
}
error.close();

BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while((line=input.readLine()) != null){
    System.out.println(line);
}

input.close();

OutputStream outputStream = p.getOutputStream();
PrintStream printStream = new PrintStream(outputStream);
printStream.println();
printStream.flush();
printStream.close();



回答5:


Try VerboseProcess from jcabi-log:

String output = new VerboseProcess(new ProcessBuilder("foo.bat")).stdout();

The class starts a background thread, listens to the output stream, and logs it.




回答6:


If you can use org.apache.commons.io.IOUTils from commons-io,

System.out.println(IOUtils.toString(process.getInputStream()));
System.err.println(IOUtils.toString(process.getErrorStream()));



回答7:


I know this is a very old question, but a better alternate for the above answers would be

ProcessBuilder builder = new ProcessBuilder(command);
builder.inheritIO();
Process p = builder.start();

From the docs of ProcessBuilder.inheritIO(),

Sets the source and destination for subprocess standard I/O to be the same as those of the current Java process.

Hope this helps someone!



来源:https://stackoverflow.com/questions/3936023/printing-runtime-exec-outputstream-to-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!