0

Lets say, that i have launched simple Java application, that outputs some strings to standart console using following command:

Runtime.getRuntime().exec("Path:/to/app.exe");

What i need is to gather all data, that launched app throws to the console. Is it possible? Thanks.

Paul.

Mark
  • 3,609
  • 1
  • 22
  • 33
johnny-b-goode
  • 3,792
  • 12
  • 45
  • 68

1 Answers1

1

You can use ProcessBuilder and get its IutputStream. Here is simple example:

public static void main(String[] args) throws Exception {
    String[] processArgs = new String[]{"ping","google.com"};
    Process process = new ProcessBuilder(processArgs).start();

    BufferedReader in = new BufferedReader(new InputStreamReader(
            //I'am using Win7 with PL encoding in console -> "CP852"
            process.getInputStream(), "CP852"));

    String line;
    while ((line = in.readLine()) != null)
        System.out.println(line);

    in.close();
    System.out.println("process ended");
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269