3

How to get the output of the command prompt which means i have opend a command prompt like this.

Process process = Runtime.getRuntime().exec("cmd /c start cmd.exe /K \"C:\\Editor\\editorTemp.exe\"");

i can not get the cmd output like this

BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));

        String line = null;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }

So how can i get the command prompt output ?

4 Answers4

1

This is not Java question. Basically what you doing is running Java (Java Main Process A) and from it starting another process (Windows CMD B). This is fine and you can get input/output streams of this process (B) in Java(A). However this process (B) starts another process (again Windows CMD C) with its own standard input/output. This process has nothing common with processes A&B and uses Windows' standard Input/Output streams. So, there are no connections between A and C. I'm not sure but I think there are some ways to run Windows CMD with different or not standard IO. Maybe something like this will work:

cmd <tty >tty

but there is no tty in Windows. Pragmatically you can do this as described here - Creating a Child Process with Redirected Input and Output but that would not work for regular CMD.

Nevertheless it became even more problematic when you start your own process from the editorTemp.exe (process D). D has even more disconnection with process A. And all for what? What don't you simply start process D directly from A and have full control on the IO streams and process itself? Here is good example how to do so.

Community
  • 1
  • 1
Alex
  • 4,457
  • 2
  • 20
  • 59
0

Your java thread is working independently of CMD call. The java code is beating the STDOUT pipe before anything is written.

If you call Process.waitFor(), it will wait until the CMD call is done. The STDOUT should be in the buffer, and then you can read it.

yxre
  • 3,576
  • 21
  • 20
  • The OT wants to see the command output, and then send something to the command input to make it continue. So you can't wait until the process is finished. Reading from the inputstream works fine even if the process is still running. – GeertPt Jul 29 '15 at 20:50
0

When you do a readLine(), your java thread is blocked until you have an actual full line or the input stream is closed.

If the program prints a partial line (without CR or LF at the end), and then waits for input, the readLine will be stuck.

So you will need to read character by character, until you think the proces has no more things to say.

See e.g. Is it possible to read from a InputStream with a timeout?

Community
  • 1
  • 1
GeertPt
  • 16,398
  • 2
  • 37
  • 61
0
import java.util.Scanner;

Inside the main write this.

Scanner output = new Scanner(System.in);

System.out.println(“Enter your name”);
String name = output.next();

If you want user to enter an int then you need to do this.

int number = output.nextInt();
4b0
  • 21,981
  • 30
  • 95
  • 142
furkanayilmaz
  • 154
  • 1
  • 11