0

I want to read input and prints only the output like

(Read 1 2 3 4 5,Prints 1 2 3 4 5) using shell execute in java

when i run the followining code output looks like this "echo 1 2 3 4 5| java task4". My class name is task4.

I don't really know what do. any suggestion?

String command="echo 1 2 3 4 5 | java task4";
Process cmdProc = Runtime.getRuntime().exec(command);


BufferedReader stdoutReader = new BufferedReader(
         new InputStreamReader(cmdProc.getInputStream()));
String line;
while ((line = stdoutReader.readLine()) != null) {
   // process procs standard output here
           System.out.println(line);
}

BufferedReader stderrReader = new BufferedReader(
         new InputStreamReader(cmdProc.getErrorStream()));
String line1;
while ((line1 = stderrReader.readLine()) != null) {
   // process procs standard error here
}


int retValue = cmdProc.exitValue();
Mandar Pandit
  • 2,171
  • 5
  • 36
  • 58
  • You can refer to the link (http://stackoverflow.com/questions/5928225/how-to-make-pipes-work-with-runtime-exec) – Mandar Pandit Jun 19 '14 at 14:05
  • I think changing the input to `exec()` command might solve your issue. Try with changing `String command="echo 1 2 3 4 5 | java task4"` to `String[] commands = {"echo 1 2 3 4 5","java task4"};`. I am not 100% sure of this, but just suggesting one point. – Mandar Pandit Jun 20 '14 at 08:11

0 Answers0