Interaction with another Java process

I am trying to interact with another process in Java. It happens like this ...

Runtime rt;
Process pr=rt.exec("cmd");

then I send some commands to the process using ...

BufferedReader processOutput = new BufferedReader(new InputStreamReader(pr.getInputStream()));
BufferedWriter processInput = new BufferedWriter(new OutputStreamWriter(pr.getOutputStream()));

processInput.write("gdb");
processInput.flush();

At the moment, I'm not interested in getting out. So I try to ignore it with.

while(processOutput.readLine() != null);

but this loop is forever. I know this because the process is still running and is not sending zero. I do not want to stop it now. I need to send commands based on user input and then get output.

How to do it? In other words, I want to reset the Process output or ignore it after executing some commands and read it only when I want.

+2
source share
1 answer

. , , , .

, :

public class ReaderThread extends Thread {

    private BufferedReader reader = null;
    public ReaderThread(BufferedReader reader) {
        this.reader = reader;
    }

    @Override
    public void run() {
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        }
        catch(IOException exception) {
            System.out.println("!!Error: " + exception.getMessage());
        }
    }
}

while(processOutput.readLine() != null); :

ReaderThread reader = new ReaderThread(processOutput);
reader.start();
+2

All Articles