我試圖打開一個shell(xterm中)與它的I/O進行交互,並與之交互(寫入命令和讀取shell的輸出)打開外殼和在java中
這裏是一個代碼示例贏得't 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());
}
當我執行這個程序時,打開一個「xterm」窗口並且不輸入「ls」命令。 只有當我關閉窗口,我得到一個「-1」印刷,並沒有什麼從外殼讀
重要 -
我知道我可以只使用:
過程PR =新的ProcessBuilder(」 。LS「)開始();
爲了得到輸出,但我需要「的xterm」打開其他用途
非常感謝
標題有誤導性。 xterm是運行shell的終端應用程序。它本身不是一個殼。 – Perception