I try to open a shell (xterm) and interact with it (write commands and read shell output)
Here is an example of code that will not work:
public static void main(String[] args) throws IOException { Process pr = new ProcessBuilder("xterm").start(); PrintWriter pw = new PrintWriter(pr.getOutputStream()); pw.println("ls"); pw.flush(); InputStreamReader in = new InputStreamReader(pr.getInputStream()); System.out.println(in.read()); }
When I run this program, the xterm window opens and the ls command is not entered. Only when I close the window do I get "-1" and nothing is read from the shell
IMPORTANT -
I know I can just use:
Process pr = new ProcessBuilder ("ls"). Start ();
To get the result, but I need an "xterm" open for other purposes
thanks a lot
source share