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 ?

8
  • not sure . try call instead of start Commented Jul 26, 2015 at 9:18
  • Is there any reason for starting another cmd and starting your program in that? Commented Jul 26, 2015 at 9:18
  • @Codebender yes.basically i need to run a exe file that exe should execute from the cmd. Commented Jul 26, 2015 at 9:21
  • @FastSnail start ? i'm not use processBuilder .. Commented Jul 26, 2015 at 9:22
  • 1
    @FastSnail its works but the cmd not prompt for the user. Thinks if i call to a program that need user input from the cmd then call option is not valueble. Commented Jul 26, 2015 at 9:30

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

AFAIK Windows CMD redirects its STDIN/OUT just fine
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.

1 Comment

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.
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?

Comments

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.