user8087992
user8087992

Reputation:

Open two terminals for reading and writing

If I start my java program in an OS native terminal (cmd.exe or xterm), is it possible to:

  1. Keep the current terminal for reading/writing as System.in and System.out
  2. Open another terminal for reading/writing as NewTerm.in and NewTerm.out

As it stands, all I can seem to do is open the second terminal. I cannot write to it (I've tried with BufferedWriter) and the only way it displays commands is if those commands were issued with its opening (i.e. Runtime.getRuntime("xterm ls") or ProcessBuilder(command).start(); where command is a String[]).

I would like to keep the two terminals open so that I can compare their outputs. The sequence would be as follows:

Is this possible?

Here is how I have opened a new terminal but cant write to it after opening:

public class InterFace {
    public static void main(String[] args) {
        try {
            Process p = Runtime.getRuntime().exec("xterm");
            BufferedWriter w = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));

            w.write("ls");
            w.flush();
            w.close();

            BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));

            String s;
            while ((s = r.readLine()) != null) {
                System.out.println(s);
            }
        }
        catch (IOException io) {
            io.printStackTrace();
        }
    }
}

Upvotes: 0

Views: 1261

Answers (1)

Duarte Meneses
Duarte Meneses

Reputation: 2938

You need the line separator after the command.

w.write("ls" + System.lineSeparator());

Upvotes: 1

Related Questions