2012-11-17 76 views
4

我試圖打開一個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」打開其他用途

非常感謝

+0

標題有誤導性。 xterm是運行shell的終端應用程序。它本身不是一個殼。 – Perception

回答

4

你的問題是,在xterm進程的標準輸入和輸出不對應實際的shell在終端窗口中可見。而不是一個xterm,你可以有更多的成功直接運行shell過程:

Process pr = new ProcessBuilder("sh").start(); 
+0

我知道這已經幾歲了,但其他人會看到這一點。該解決方案本身不起作用。對於你想要的工作,你需要添加-i以使其交互。在我意識到我應該檢查/ bin/sh的文檔之前,我一直對着我的頭撞了一個小時http://pubs.opengroup.org/onlinepubs/009695399/utilities/sh.html – flips

1

Here如何與shelljava 8交互的完整的Java主要的例子(是非常簡單的做Java的任何4,5,6 )

輸出

$ javac Main.java 
$ java Main 
echo "hi" 
hi 

的代碼的實施例

import java.io.*; 
import java.util.Arrays; 
import java.util.List; 
import java.util.Scanner; 


public class Main { 

    public static void main(String[] args) throws IOException, InterruptedException { 

     final List<String> commands = Arrays.asList("/bin/sh"); 
     final Process p = new ProcessBuilder(commands).start(); 

     // imprime erros 
     new Thread(() -> { 
      BufferedReader ir = new BufferedReader(new InputStreamReader(p.getErrorStream())); 
      String line = null; 
      try { 
       while((line = ir.readLine()) != null){ 
        System.out.printf(line); 
       } 
      } catch(IOException e) {} 
     }).start(); 

     // imprime saida 
     new Thread(() -> { 
      BufferedReader ir = new BufferedReader(new InputStreamReader(p.getInputStream())); 
      String line = null; 
      try { 
       while((line = ir.readLine()) != null){ 
        System.out.printf("%s\n", line); 
       } 
      } catch(IOException e) {} 
     }).start(); 

     // imprime saida 
     new Thread(() -> { 
      int exitCode = 0; 
      try { 
       exitCode = p.waitFor(); 
      } catch(InterruptedException e) { 
       e.printStackTrace(); 
      } 
      System.out.printf("Exited with code %d\n", exitCode); 
     }).start(); 


     final Scanner sc = new Scanner(System.in); 
     final BufferedWriter bf = new BufferedWriter(new OutputStreamWriter(p.getOutputStream())); 
     final String newLine = System.getProperty("line.separator"); 
     while(true){ 
      String c = sc.nextLine(); 
      bf.write(c); 
      bf.newLine(); 
      bf.flush(); 
     } 

    } 

}