2014-01-24 116 views
0

所以我有這樣的代碼:執行命令的過程

String[] command = new String[] { "java", "-jar", file.getName() }; 
try { 
    System.out.println("Loading input/output"); 
    final Process process = Runtime.getRuntime().exec(command); 
    BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())); 
    final OutputStreamWriter out = new OutputStreamWriter(process.getOutputStream()); 
    final BufferedReader consolein = new BufferedReader(new InputStreamReader(System.in)); 
    System.out.println("Loaded input/output"); 
    new Thread() { 
     @Override 
     public void run() { 
      while (true) { 
       System.out.println("hi"); 
       try { 
        String temp = consolein.readLine(); 
        out.write(temp); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    }.start(); 
    while (true) { 
     System.out.println(in.readLine()); 
    } 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

,我試圖執行命令的運行罐子。輸入很好,我看到控制檯中的所有消息。我添加了「hi」消息來測試它是否工作。無論什麼時候我輸入什麼都會顯示但是,無論我輸入什麼內容都不會被髮送到正在運行的程序。我在這裏做錯了什麼?

+0

你有沒有嘗試添加一個新行輸入的結束? – MadProgrammer

+0

正如\ n?或者是什麼? – PaulBGD

回答

0

我做了一個快速測試。我寫了這個小程序,它是由我的其他類中運行...

import java.util.Scanner; 

public class RunMe { 

    public static void main(String[] args) { 
     System.out.println("Hello world"); 
     Scanner scanner = new Scanner(System.in); 
     String text = scanner.nextLine(); 
     System.out.println("You said: " + text); 
     System.out.println("Bye!"); 
    } 

} 

在我寫這篇運行它...

import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 

public class Runner { 

    private static BufferedReader br; 
    private static BufferedWriter bw; 

    public static void main(String[] args) { 
     ProcessBuilder pb = new ProcessBuilder("java", "-jar", "RunMe.jar"); 
     pb.redirectError(); 

     Scanner scanner = new Scanner(System.in); 
     try { 
      Process p = pb.start(); 
      br = new BufferedReader(new InputStreamReader(p.getInputStream())); 
      bw = new BufferedWriter(new OutputStreamWriter(p.getOutputStream())); 

      new Thread(new Runnable() { 
       @Override 
       public void run() { 
        try { 
         String text = null; 
         while ((text = br.readLine()) != null) { 
          System.out.println(text); 
         } 
        } catch (IOException ex) { 
         ex.printStackTrace(); 
        } 
       } 
      }).start(); 

      String text = scanner.nextLine(); 
      bw.write(text); 
      bw.newLine(); 
      bw.flush(); 

      p.waitFor(); 

     } catch (IOException | InterruptedException ex) { 
      ex.printStackTrace(); 
     } finally { 
      try { 
       br.close(); 
      } catch (Exception e) { 
      } 
      try { 
       bw.close(); 
      } catch (Exception e) { 
      } 
     } 
    } 

} 

它輸出...

Hello world 
This is a test 
You said: This is a test 
Bye! 

(第二行是我輸入的內容,第三行是RunMe類的輸出)。

所以我建議你需要發送一個新的行。您可以嘗試使用\n,但最好使用line.separator屬性