2016-11-29 12 views
1

私はいくつかのコマンドを持つ1つのシェルスクリプトを持っていますが、そのうちの1つは実行時にいくつかのデータを入力する予定です。 exec()メソッドを使ってこのシェルスクリプトを実行しています。現在、入力を求められたら手動でデータを入力しています。コンソールはのデータが入力されることを期待するときは、次の私が何をしたいのかJavaプログラムで実行時にコンソールからデータを書き込み、読み取る

  Process p = Runtime.getRuntime().exec("myshellscript"); 

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

      BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream())); 

      // read the output from the command 
      System.out.println("Here is the standard output of the command:\n"); 
      while ((s = stdInput.readLine()) != null) { 
       System.out.println(s); 
      } 

      // read any errors from the attempted command 
      System.out.println("Here is the standard error of the command (if any):\n"); 
      while ((s = stdError.readLine()) != null) { 
       System.out.println(s); 
      } 

      System.exit(0); 

、私のコードですが、それはJavaプログラムによって入力されたし、ちょうど私の更なるJavaプログラムを継続する必要があります。

答えて

2

ここで私は何をすべきかです:

  1. 2つのp.getInputStreamから読まれるスレッド()とp.getErrorStream()、
  2. を作成するには、(p.getOutputStreamにデータを書き込む)、
  3. (join()メソッドを使用)を終了するために、2つのスレッドを待つ

EDIT:

public class Main { 
    private static final Logger LOG = Logger.getLogger(Main.class.getName()); 

    public static void main(String[] args) { 
     try { 
      Process p = Runtime.getRuntime().exec("myshellscript"); 
      Thread outHandler = new OutputHandler(p.getInputStream(), "UTF-8"); 
      outHandler.setDaemon(true); 
      outHandler.start(); 
      Thread errHandler = new OutputHandler(p.getErrorStream(), "UTF-8"); 
      errHandler.setDaemon(true); 
      errHandler.start(); 
      sendInput(p, "the input data", "UTF-8"); 
      int result = p.waitFor(); 
      outHandler.join(); 
      errHandler.join(); 
      System.out.println("exit code: " + result); 
     } catch (IOException ex) { 
      LOG.log(Level.SEVERE, null, ex); 
     } catch (InterruptedException ex) { 
      LOG.log(Level.SEVERE, null, ex); 
     } 
    }  

private void sendInput(Process process, String input, String encoding) 
     throws IOException { 

    try (OutputStream stream = process.getOutputStream(); 
      Writer writer = new OutputStreamWriter(
        stream, encoding == null ? "UTF-8" : encoding); 
      PrintWriter out = new PrintWriter(writer)) { 
     if (input != null) { 
      Reader reader = new StringReader(input); 
      BufferedReader in = new BufferedReader(reader); 
      String line = in.readLine(); 
      while (line != null) { 
       out.println(line); 
       line = in.readLine(); 
      } 
     } 
    } 
} 

    private static class OutputHandler extends Thread { 
     private BufferedReader in; 

     private OutputHandler(InputStream in, String encoding) 
       throws UnsupportedEncodingException { 
      this.in = new BufferedReader(new InputStreamReader(
        in, encoding == null ? "UTF-8" : encoding)); 
     } 

     @Override 
     public void run() { 
      try { 
       String s = in.readLine(); 
       while (s != null) { 
        System.out.println(s); 
        s = in.readLine(); 
       } 
      } catch (IOException ex) { 
       LOG.log(Level.SEVERE, null, ex); 
      } finally { 
       try { 
        in.close(); 
       } catch (IOException ex) { 
        LOG.log(Level.SEVERE, null, ex); 
       } 
      } 
     } 
    } 
} 
+0

あなたはjavaに新しいコードを書いてください。私はマルチスレッドでは一度も働いていません。 –

+1

ここに行きます.... –

+0

ProcessBuilderを使用していた場合は、redirectErrorStream(true)を実行でき、スレッドが1つだけ必要であることに注意してください。 –

関連する問題